【问题标题】:How to customize this drawable with parameters in Android?如何在Android中使用参数自定义这个drawable?
【发布时间】:2018-02-02 11:23:07
【问题描述】:

这是我用作按钮背景的“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>

这是一个用例

<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"/>

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

例如通过 Java

MyDrawable myDrawable=new MyDrawable();
myDrawable.setGradientColors(color1, color2);
myDrawable.setCornerRadius(size);
button.setBackground(Drawable);

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

 <MyButton 
      parameter_gradientcolor1:@color/color1
      parameter_gradientcolor2:@color/color2
      ... />

编辑

这不起作用,对点击事件的反应和正确的渐变都没有

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);
}
}

这是在布局中

 <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" />

【问题讨论】:

    标签: android xml android-custom-view android-drawable android-launcher


    【解决方案1】:

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

    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);
        }
    
    }
    

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

    编辑

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

    yourProject/app/src/main/res/values 目录中,创建一个名为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>
    

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

    <SelectorButton
        app:normalStartColor=""
        app:normalEndColor=""
        app:pressedStartColor=""
        app:pressedEndColor=""
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/> 
    

    编辑:从 xml 中获取值

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

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

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

        mStateListDrawable.addState(new int[]{android.R.attr.state_pressed, -android.R.attr.state_enabled}, pressedDrawable);
    

    【讨论】:

    • 谢谢,你能告诉我你如何在 xml 中设置这个按钮的样式吗?样式我没有考虑,只是想在需要特定渐变的时候直接在布局中设置这几个参数。
    • @user3290180 看看新答案。
    • 谢谢,为了完成工作我要问你两个问题:可能最后一个指令不完整,因为最后一个参数丢失,应该这样吗? mStateListDrawable.addState(new int[]{-android.R.attr.state_pressed, -android.R.attr.state_enabled}, normalDrawable);从构造函数的 attrs 变量中检索这些颜色属性的值的正确方法是什么?
    • 它们是整数吗?做 attrs.getAttributeIntValue("app", "normalStartColor", 0) 是否正确?
    【解决方案2】:

    xml中的drawable只是一个drawable,所以更像是一个图像,你不能这样设置它的样式。

    但是,您可以将自定义样式添加到自定义按钮 MyButton。 可以从这里学到https://developer.android.com/training/custom-views/create-view.html#customattr

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-06
      相关资源
      最近更新 更多