【问题标题】:Android Studio: Create Object in custom SwipeButton with attributes from XMLAndroid Studio:使用 XML 中的属性在自定义 SwipeButton 中创建对象
【发布时间】:2019-08-11 11:16:51
【问题描述】:
public class SwipeButton extends RelativeLayout {


private ImageView swipeButtonInner;
private float initialX;
private boolean active;
private TextView centerText;
private ViewGroup background;

private Drawable disabledDrawable;
private Drawable enabledDrawable;

private OnStateChangeListener onStateChangeListener;
private OnActiveListener onActiveListener;

private static final int ENABLED = 0;
private static final int DISABLED = 1;

private int collapsedWidth;
private int collapsedHeight;

private LinearLayout layer;
private boolean trailEnabled = false;
private boolean hasActivationState;

public SwipeButton(Context context) {
    super(context);

    init(context, null, -1, -1);
}

public SwipeButton(Context context, AttributeSet attrs) {
    super(context, attrs);

    init(context, attrs, -1, -1);
}

public SwipeButton(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);

    init(context, attrs, defStyleAttr, -1);
}

@TargetApi(21)
public SwipeButton(Context context, AttributeSet attrs, int defStyleAttr, int 
defStyleRes) {
    super(context, attrs, defStyleAttr, defStyleRes);

    init(context, attrs, defStyleAttr, defStyleRes);
}

public boolean isActive() {
    return active;
}

public void setText(String text) {
    centerText.setText(text);
}

public void setBackground(Drawable drawable) {
    background.setBackground(drawable);
}

public void setSlidingButtonBackground(Drawable drawable) {
    background.setBackground(drawable);
}

public void setDisabledDrawable(Drawable drawable) {
    disabledDrawable = drawable;

    if (!active) {
        swipeButtonInner.setImageDrawable(drawable);
    }
}

public void setButtonBackground(Drawable buttonBackground) {
    if (buttonBackground != null) {
        swipeButtonInner.setBackground(buttonBackground);
    }
}

public void setEnabledDrawable(Drawable drawable) {
    enabledDrawable = drawable;

    if (active) {
        swipeButtonInner.setImageDrawable(drawable);
    }
}

public void setOnStateChangeListener(OnStateChangeListener 
onStateChangeListener) {
    this.onStateChangeListener = onStateChangeListener;
}

public void setOnActiveListener(OnActiveListener onActiveListener) {
    this.onActiveListener = onActiveListener;
}

public void setInnerTextPadding(int left, int top, int right, int bottom) {
    centerText.setPadding(left, top, right, bottom);
}

public void setSwipeButtonPadding(int left, int top, int right, int bottom) {
    swipeButtonInner.setPadding(left, top, right, bottom);
}

public void setHasActivationState(boolean hasActivationState) {
    this.hasActivationState = hasActivationState;
}

我的 XML:`

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:id="@+id/AnswerRelativeLayout"
    android:layout_height="70dp"
    android:orientation="vertical"
    tools:context="com.example.dynamicobj.FragmentMain">


    <com.example.dynamicobj.SwipeButton
        android:id="@+id/test_btn"
        android:layout_width="match_parent"
        android:layout_height="70dp"
        app:button_background="@drawable/shape_button"
        app:button_image_disabled="@drawable/ic_launcher_background"
        app:button_image_height="60dp"
        app:button_image_width="100dp"
        app:has_activate_state="true"
        app:initial_state="disabled"
        app:inner_text="Termine buchen"
        app:inner_text_background="@drawable/shape_rounded"
        app:inner_text_bottom_padding="18dp"
        app:inner_text_right_padding="200dp"
        app:inner_text_color="@android:color/black"
        app:inner_text_size="16sp"
        app:inner_text_top_padding="18dp" />

    </LinearLayout>`

F

ragmentMain:

 public class FragmentMain extends Fragment {

    Context context;
    View rootView;

    public FragmentMain() {
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(false);
    }

    @Override
    public View onCreateView(final LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_main, container, false);

        LinearLayout layout = (LinearLayout) 
    rootView.findViewById(R.id.AnswerRelativeLayout);
        SwipeButton button = new SwipeButton(getContext());
        layout.addView(button);

        return rootView;
        }
    }    

所以我从 Git (com.ebanx.swipebtn.SwipeButton) 获得了这个自定义 SwipeButton 类。现在我想从 SwipeButton 创建一个对象,其布局类似于 xml 文件中的布局。有什么方法可以让新按钮具有预先完成的布局,而不必在 SwipeButton 类中使用所有这些方法?稍后我将动态创建按钮,但都是相同的布局。请帮忙?

【问题讨论】:

    标签: android button layout swipe


    【解决方案1】:

    您忘记将LayoutParams 添加到您的button

    在将按钮添加到布局之前使用下面的代码

    button.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT))
    

    并设置您的LinearLayout 高度wrap_content 而不是70dp

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-01-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-18
      • 1970-01-01
      相关资源
      最近更新 更多