【问题标题】:Change Button custom shape states color programmatically以编程方式更改按钮自定义形状状态颜色
【发布时间】:2016-10-11 06:10:33
【问题描述】:

这个网站上有很多关于更改按钮颜色的答案,但我没有设法在我的案例中使用。

我希望能够动态更改按钮的颜色,该按钮仍然需要在按下时有视觉反馈,并且它需要圆角。

圆角部分是最近才决定的,所以之前我用过这样的东西:

    StateListDrawable states = new StateListDrawable();
    states.addState(new int[]{android.R.attr.state_pressed},
         new ColorDrawable(hsvDarkenColor(theme.get_buttonsBgColor())));
         states.addState(new int[]{android.R.attr.state_focused},
        new ColorDrawable(hsvDarkenColor(theme.get_buttonsBgColor())));
         states.addState(new int[]{},
       new ColorDrawable(Color.parseColor(theme.get_buttonsBgColor())));
     ((Button) button).setBackgroundDrawable(states);

//this is for the focused/pressed state of the button
   private static int hsvDarkenColor(String originalColor)
        {
            float[] hsv = new float[3];
            int color = Color.parseColor(originalColor);
            Color.colorToHSV(color, hsv);
            hsv[2] *= 0.8f; // value component
            return Color.HSVToColor(hsv);
        }

但是,这不会保留按钮的圆角形状,而是将它们变成正方形。

我的默认按钮的背景是一个状态列表,每个状态都有自己的可绘制对象,用于按下和未按下等。

在styles.xml中:

    <style name="xx.Light.ScanButton" parent="android:style/Widget.Button">
    <item name="android:focusable">true</item>
    <item name="android:background">@drawable/xx_scan_button</item>
    <item name="android:textStyle">normal</item>
    <item name="android:textColor">@color/text_light</item>
    </style>

在drawable/xx_scan_button.xml中:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Non focused states -->
<item
    android:state_focused="false"
    android:state_selected="false"
    android:state_pressed="false"
    android:drawable="@drawable/xx_scan_button_normal"
    />
<item
    android:state_focused="false"
    android:state_selected="true"
    android:state_pressed="false"
    android:drawable="@drawable/xx_scan_button_pressed"
    />

<!-- Focused states -->
<item
    android:state_focused="true"
    android:state_selected="false"
    android:state_pressed="false"
    android:drawable="@drawable/xx_scan_button_pressed"
    />
<item
    android:state_focused="true"
    android:state_selected="true"
    android:state_pressed="false"
    android:drawable="@drawable/xx_scan_button_pressed"
    />

<!-- Pressed -->
<item
    android:state_pressed="true"
    android:drawable="@drawable/xx_scan_button_pressed"
    />

<!-- Disabled -->
<item
    android:state_enabled="false"
    android:drawable="@drawable/xx_scan_button_normal"
    />
</selector>

在drawable/xx_scan_button_normal.xml中

<shape xmlns:android="http://schemas.android.com/apk/res/android"             android:shape="rectangle" >
<corners android:bottomRightRadius="5dp"
         android:bottomLeftRadius="0dp"
         android:topLeftRadius="0dp"
         android:topRightRadius="5dp"/>
<solid
    android:color="#C74700"
    />
<padding
    android:left="0dp"
    android:top="0dp"
    android:right="0dp"
    android:bottom="0dp"
    />
<size
    android:width="270dp"
    android:height="60dp"
    />
</shape>

TL;DR:我需要某种方法从按钮的可绘制状态列表中提取可绘制形状,以更改其颜色。 或者,如果你们有更好的解决方案,我会全力以赴。

【问题讨论】:

    标签: java android xml button colors


    【解决方案1】:

    您必须为单独的颜色制作单独的可绘制对象,并以编程方式更改可绘制对象而不是颜色。从程序更改颜色会覆盖drawble。

    【讨论】:

    • 感谢您的回复。但是,我需要让用户从非常广泛的颜色中选择自己的颜色。我将无法为所有这些创建可绘制对象。 (不是我的决定:))
    猜你喜欢
    • 1970-01-01
    • 2019-05-29
    • 1970-01-01
    • 1970-01-01
    • 2010-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-16
    相关资源
    最近更新 更多