如何在Android中使用参数自定义这个drawable?

use*_*180 5 xml android android-custom-view android-launcher android-drawable

这是我用作按钮背景的“mydrawable”的 xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="false" android:state_pressed="false" >
       <shape xmlns:android="http://schemas.android.com/apk/res/android"
            android:shape="rectangle">
            <gradient
                android:angle="-45"
                android:endColor="@color/colorPrimary700"
                android:startColor="@color/colorPrimary600"
                android:type="linear" />
            <corners android:radius="@dimen/ic_button_corner"></corners>
        </shape>
</item>
<item android:state_focused="false" android:state_pressed="true" >
        <shape xmlns:android="http://schemas.android.com/apk/res/android"
            android:shape="rectangle">
            <gradient
                android:angle="-45"
                android:endColor="@color/colorPrimary800"
                android:startColor="@color/colorPrimary700"
                android:type="linear" />
            <corners android:radius="@dimen/ic_button_corner"></corners>
        </shape>
</item>
</selector>
Run Code Online (Sandbox Code Playgroud)

这是一个用例

<Button
            android:id="@+id/login_button"
            style="?android:textAppearanceSmall"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/mydrawable"
            android:text="@string/log_in"/>
Run Code Online (Sandbox Code Playgroud)

如何将此静态可绘制对象移植到具有可配置参数(例如每个项目的渐变中使用的颜色和大小角半径)的自定义视图中?

例如通过 Java

MyDrawable myDrawable=new MyDrawable();
myDrawable.setGradientColors(color1, color2);
myDrawable.setCornerRadius(size);
button.setBackground(Drawable);
Run Code Online (Sandbox Code Playgroud)

是否也可以通过自定义按钮(MyButton,而不是 MyDrawable)?

 <MyButton 
      parameter_gradientcolor1:@color/color1
      parameter_gradientcolor2:@color/color2
      ... />
Run Code Online (Sandbox Code Playgroud)

编辑

这是行不通的,既没有对点击事件的反应,也没有正确的渐变

public class SelectorButton extends AppCompatButton {

StateListDrawable mStateListDrawable;

public SelectorButton(Context context, AttributeSet attrs) {
    super(context, attrs);
    float cornerRadius = attrs.getAttributeFloatValue("app", "cornerRadius", 0);
    int normalStartColor = attrs.getAttributeIntValue("app", "normalStartColor", R.color.mds_grey_400);
    int normalEndColor = attrs.getAttributeIntValue("app", "normalEndColor", R.color.mds_grey_500);
    int pressedStartColor = attrs.getAttributeIntValue("app", "pressedStartColor", R.color.mds_grey_400);
    int pressedEndColor = attrs.getAttributeIntValue("app", "pressedEndColor", R.color.mds_grey_500);

    GradientDrawable normalDrawable = new GradientDrawable(
            GradientDrawable.Orientation.TOP_BOTTOM,
            new int[]{normalStartColor, normalEndColor});
    normalDrawable.setCornerRadius(cornerRadius);
    GradientDrawable pressedDrawable = new GradientDrawable(
            GradientDrawable.Orientation.TOP_BOTTOM,
            new int[]{pressedStartColor, pressedEndColor});
    pressedDrawable.setCornerRadius(cornerRadius);

    mStateListDrawable = new StateListDrawable();
    mStateListDrawable.addState(new int[]{-android.R.attr.state_pressed, -android.R.attr.state_focused},
            normalDrawable);
    mStateListDrawable.addState(new int[]{android.R.attr.state_pressed, -android.R.attr.state_focused},
            pressedDrawable);
    setBackground(mStateListDrawable);
}
}
Run Code Online (Sandbox Code Playgroud)

这是布局中的

 <com.utils.views.SelectorButton
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Login"
    android:clickable="true"
    app:normalEndColor="@color/mds_blue_400"
    app:normalStartColor="@color/mds_red_500"
    app:pressedEndColor="@color/mds_amber_500"
    app:pressedStartColor="@color/mds_green_300" />
Run Code Online (Sandbox Code Playgroud)

CoX*_*ier 2

是的,您可以通过自定义按钮执行此操作。这是一个代码片段。

public class SelectorButton extends AppCompatButton {
    StateListDrawable mStateListDrawable;

    public SelectorButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        mStateListDrawable = new StateListDrawable();
        GradientDrawable normalDrawable = new GradientDrawable(yourColor);
        normalDrawable.setCornerRadius(yourRadius);
        mStateListDrawable.addState(
                new int[]{-android.R.attr.state_pressed, -android.R.attr.state_enabled}, );
        setBackground(mStateListDrawable);
    }

}
Run Code Online (Sandbox Code Playgroud)

为了通过 XML 设置样式,您可以在 中定义自定义样式,例如颜色或圆角半径attrs.xml。如果您有任何问题,请随时询问。

编辑

现在我将向您展示如何在 XML 中声明自定义样式并使用它们。例如,我想设置正常和按下状态渐变颜色。

yourProject/app/src/main/res/valuesdir 中,创建一个名为attrs.xml.

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="SelectorButton">
        <attr name="normalStartColor" format="color"/>
        <attr name="normalEndColor" format="color"/>

        <attr name="pressedStartColor" format="color"/>
        <attr name="pressedEndColor" format="color"/>
    </declare-styleable>
</resources>
Run Code Online (Sandbox Code Playgroud)

如您所见,我定义了四个属性。现在您可以通过 xml 设置这些属性。

<SelectorButton
    app:normalStartColor=""
    app:normalEndColor=""
    app:pressedStartColor=""
    app:pressedEndColor=""
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/> 
Run Code Online (Sandbox Code Playgroud)

编辑:从 xml 获取值

对我的错误感到抱歉。您可以这样获取这些值。

<SelectorButton
    app:normalStartColor=""
    app:normalEndColor=""
    app:pressedStartColor=""
    app:pressedEndColor=""
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/> 
Run Code Online (Sandbox Code Playgroud)

并且有一个按下状态。你可以这样做。

    final TypedArray a = context.obtainStyledAttributes(
            attrs, R.styleable.SelectorButton, 0, 0);
    int normalStartColor = a.getColor(R.styleable.SelectorButton_normalStartColor, 0);
    a.recycle();
Run Code Online (Sandbox Code Playgroud)