Android Studio:使用XML中的属性在自定义SwipeButton中创建对象

问题描述 投票:0回答:1
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;
}

我们是CML:ё

    <?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

ragment DOMAIN:

 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创建一个Object,其布局与xml文件中的布局类似。有没有方法,我可以给新按钮预先完成布局而不必在SwipeButton类中使用所有这些方法?我将在稍后动态创建按钮,但所有都是相同的布局。帮忙吗?

android button layout swipe
1个回答
0
投票

你忘了把LayoutParams添加到你的button

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

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

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

© www.soinside.com 2019 - 2024. All rights reserved.