Android themes change checkbox item text color

2020-02-10 21:09发布

How do I just change the text color of checkbox views in Android Studio's styles.xml?

I want to create a theme where all checkbox items are a color different from the default black like:

enter image description here

Except I want the box itself to be the same color as the text.

Should I be using the following in my theme?

<item name="android:checkboxStyle">@style/App_CheckboxStyle</item>

6条回答
虎瘦雄心在
2楼-- · 2020-02-10 21:31

The solution for me was to use textColor property:

<CheckBox
android:textColor="@color/mycolor"
...
/>
查看更多
放我归山
3楼-- · 2020-02-10 21:39

There is no attribute has impact on Checkbox text color at theme level.

All you can do to change text color is to define a style as shown below and apply it to checkbox using style attribute.

<style name="MyChkTextColor">
    <item name="android:textColor">#7ca011</item>4
</style>

http://www.zoftino.com/android-ui-control-checkbox

查看更多
smile是对你的礼貌
4楼-- · 2020-02-10 21:43

You can change color by using drawable like this

<?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>

in you layout just use button property

<CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:button="@drawable/custom_checkbox"
        android:id="@+id/checkBox" />
查看更多
家丑人穷心不美
5楼-- · 2020-02-10 21:52

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

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>

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.

查看更多
可以哭但决不认输i
6楼-- · 2020-02-10 21:52

You can use android:textColorPrimaryDisableOnly for change CheckBox/RadioButton textColor and colorAccent for change the drawable

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="colorAccent">#00f</item> <!-- blue -->
    <item name="android:textColorPrimaryDisableOnly">#f00</item> <!-- red -->
</style>

enter image description here

查看更多
干净又极端
7楼-- · 2020-02-10 21:55

You can change the color of the checkbox, using this:

<item name="colorAccent">@color/green</item>

You can change the color of the textView in the checkbox, using this:

<item name="android:textColorSecondary">@color/red</item>

For example:

<style name="SampleTheme" parent="Theme.AppCompat">
    <item name="colorAccent">@color/green</item>
    <item name="android:textColorSecondary">@color/red</item>
</style>

Else, create 3 xml files for customized colors as mentioned in this answer (look where it says Second Method).

查看更多
登录 后发表回答