我正在创建一个普通的复选框 View :
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
这个浅绿色 (#A5D6A7) 是由于主样式中定义的强调色:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorAccent">@color/green_light</item>
我已经发现我无法在运行时更改此样式: How to set colorAccent in code?
我想在特定复选框上更改此颜色,而不是在应用程序上全局更改。我可以在不创建特定 Assets 的情况下做到这一点吗?因为用户将能够在运行时更改此颜色。
最佳答案
下面的代码将顺利运行,而不会减慢复选框的检查和取消选中行为。因为 checkbox.setSupportButtonTintList(colorStateList);在某些设备上会出现异常
ColorStateList colorStateList = new ColorStateList(
new int[][]{
new int[]{-android.R.attr.state_checked}, // unchecked
new int[]{android.R.attr.state_checked} , // checked
},
new int[]{
Color.parseColor("#cccccc"),
Color.parseColor("##cccccc"),
}
);
CompoundButtonCompat.setButtonTintList(checkBox,colorStateList)
关于android - 在运行时以编程方式更改复选框 colorAccent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28800451/