繁体   English   中英

以编程方式更改按钮自定义形状状态颜色

[英]Change Button custom shape states color programmatically

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

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

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

    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:我需要某种方法从按钮的可绘制状态列表中提取可绘制形状,以便更改其颜色。 或者,如果你们有更好的解决方案,我全神贯注。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM