且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Android主题更改复选框项目文本颜色

更新时间:2023-01-27 20:12:52

我知道这个问题是很久以前提出的,但是我想添加我的解决方案.它适用于所有SDK版本.

I know that this question is asked long time ago but I want to add my solution. It works with all SDK versions.

您是正确的,为了在整个项目中更改CheckBox样式,您必须在主题中添加以下行:

You are right that in order to change CheckBox style in the whole project, you have to add the line to your theme:

<style name="AppTheme" parent="Theme.AppCompat">
    ...
    <item name="android:checkboxStyle">@style/MyCheckBoxStyle</item>
    ...
</style>

样式本身,您可以在其中设置自定义文本和复选框的颜色:

And the style itself, where you set your custom text and checkbox color:

<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属性.如果删除它,该属性将链接到支持库.这就是系统将buttonTintandroid:buttonTint视为不同参数的原因.如果您的最低SDK为21,则无需使用支持库,而只需使用android:buttonTint.

Note: android: prefix indicates that a property is linked to SDK attributes. If you remove it, the property links to support library. That's why system treats buttonTint and android:buttonTint as different parameters. If your minimum SDK is 21, you don't need to use support library and you can simply use android:buttonTint.