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

时间:2015-10-10 04:31:43

标签: android android-theme

如何在Android Studio的styles.xml中更改复选框视图的文本颜色?

我想创建一个主题,其中所有复选框项目的颜色与默认黑色不同,如:

enter image description here

除了我希望盒子本身与文本颜色相同。

我应该在我的主题中使用以下内容吗?

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

6 个答案:

答案 0 :(得分:11)

我知道很久以前就会问这个问题,但我想补充一下我的解决方案。它适用于所有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属性。如果删除它,该属性将链接到支持库。这就是系统将buttonTintandroid:buttonTint视为不同参数的原因。如果您的最低SDK为21,则无需使用支持库,只需使用android:buttonTint

答案 1 :(得分:4)

您可以使用以下方法更改checkbox的颜色:

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

您可以使用以下内容更改textViewcheckbox的颜色:

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

否则,为in this answer提及的自定义颜色创建3个xml文件(看看第二种方法)。

答案 2 :(得分:4)

我的解决方案是使用textColor属性:

<CheckBox
android:textColor="@color/mycolor"
...
/>

答案 3 :(得分:2)

您可以使用android:textColorPrimaryDisableOnly进行更改CheckBox / RadioButton textColorcolorAccent更改可绘制

<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

答案 4 :(得分:1)

你可以像这样使用drawable来改变颜色

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

答案 5 :(得分:1)

在主题级别没有属性对Checkbox文本颜色有影响。

您可以做的更改文字颜色的方法是定义如下所示的样式,并使用样式属性将其应用于复选框。

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

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