Java 以编程方式更改按钮自定义形状状态颜色
在这个网站上有很多关于改变按钮颜色的答案,但没有一个是我在我的案例中使用的 我希望能够动态地改变按钮的颜色,按钮仍然需要在按下时有视觉反馈,并且需要圆角 圆角部分是最近决定的,所以之前我使用了类似的东西:Java 以编程方式更改按钮自定义形状状态颜色,java,android,xml,button,colors,Java,Android,Xml,Button,Colors,在这个网站上有很多关于改变按钮颜色的答案,但没有一个是我在我的案例中使用的 我希望能够动态地改变按钮的颜色,按钮仍然需要在按下时有视觉反馈,并且需要圆角 圆角部分是最近决定的,所以之前我使用了类似的东西: StateListDrawable states = new StateListDrawable(); states.addState(new int[]{android.R.attr.state_pressed}, new ColorDrawable(hsv
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>
真的
@可拖动/xx\u扫描\u按钮
正常的
@颜色/文字灯
在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\u scan\u button\u 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:我需要某种方法从按钮的可绘制状态列表中提取可绘制的形状,以便更改其颜色。
或者,如果你们有更好的解决方案,我洗耳恭听。你们必须为不同的颜色制作不同的可绘制图形,并通过编程方式更改可绘制图形,而不是更改颜色。从程序更改颜色将覆盖绘图。感谢您的回复。但是,我需要让用户从非常广泛的颜色数组中选择自己的颜色。我不可能为他们所有人都创建可绘制的。(这不是我的决定:))