如何在Android Studio styles.xml中更改复选框视图的文本颜色?
我想创建一个主题,其中所有复选框项都是与默认黑色不同的颜色,如:
除了我希望盒子本身与文本颜色相同。
我应该在我的主题中使用以下内容吗?
<item name="android:checkboxStyle">@style/App_CheckboxStyle</item>
我知道很久以前就问过这个问题,但我想补充一下我的解决方案。它适用于所有SDK版本。
你是对的,为了在整个项目中改变CheckBox样式,你必须在你的主题中添加这一行:
<style name="AppTheme" parent="Theme.AppCompat">
...
<item name="android:checkboxStyle">@style/MyCheckBoxStyle</item>
...
</style>
还有样式本身,您可以在其中设置自定义文本和复选框颜色:
<style name="MyCheckBoxStyle" parent="Widget.AppCompat.CompoundButton.CheckBox">
<item name="android:textColor">@android:color/holo_purple</item>
<item name="buttonTint">@android:color/holo_purple</item>
</style>
注意:android:
前缀表示属性链接到SDK属性。如果删除它,该属性将链接到支持库。这就是为什么系统将buttonTint
和android:buttonTint
视为不同的参数。如果您的最低SDK为21,则无需使用支持库,只需使用android:buttonTint
即可。
您可以使用以下命令更改checkbox
的颜色:
<item name="colorAccent">@color/green</item>
您可以使用以下命令更改textView
中checkbox
的颜色:
<item name="android:textColorSecondary">@color/red</item>
例如:
<style name="SampleTheme" parent="Theme.AppCompat">
<item name="colorAccent">@color/green</item>
<item name="android:textColorSecondary">@color/red</item>
</style>
否则,如qazxsw poi所述,为自定义颜色创建3个qazxsw poi文件(看看它的第二种方法)。
我的解决方案是使用textColor属性:
xml
您可以使用in this answer更改<CheckBox
android:textColor="@color/mycolor"
...
/>
/ android:textColorPrimaryDisableOnly
CheckBox
和RadioButton
以更改drawable
textColor
colorAccent
在主题级别没有属性对Checkbox文本颜色有影响。
您可以执行的更改文本颜色的方法是定义如下所示的样式,并使用样式属性将其应用于复选框。
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true"
android:drawable="@drawable/cbchk_blue"
android:state_focused="false">
</item>
<item android:state_checked="true"
android:drawable="@drawable/cbchk_blue"
android:state_focused="true">
</item>
<item android:state_checked="false"
android:drawable="@drawable/cbunchk_blue"
android:state_focused="false">
</item>
<item android:state_checked="false"
android:drawable="@drawable/cbunchk_blue"
android:state_focused="true">
</item>
</selector>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="@drawable/custom_checkbox"
android:id="@+id/checkBox" />