【问题标题】:Android themes change checkbox item text colorAndroid主题更改复选框项目文本颜色
【发布时间】:2015-10-10 04:31:43
【问题描述】:

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

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

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

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

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

【问题讨论】:

    标签: android android-theme


    【解决方案1】:

    我知道很久以前有人问过这个问题,但我想添加我的解决方案。它适用于所有 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

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        【解决方案3】:

        您可以使用android:textColorPrimaryDisableOnly 更改CheckBox/RadioButton textColorcolorAccent 更改drawable

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

        【讨论】:

        • 终于!我一直在网上寻找这个。非常感谢!
        【解决方案4】:

        你可以改变checkbox的颜色,使用这个:

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

        你可以改变checkboxtextView的颜色,使用这个:

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

        否则,创建 3 个 xml 文件,用于自定义颜色,如 in this answer 所述(看看上面写着 Second Method 的位置)。

        【讨论】:

        • 这不正确... textColorSecondary 设置未选中复选框的颜色。它不会改变相关文本的颜色。
        【解决方案5】:

        你可以像这样使用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" />
        

        【讨论】:

          【解决方案6】:

          在主题级别没有对复选框文本颜色有影响的属性。

          你可以做的就是改变文本颜色,如下所示定义一个样式,并使用样式属性将它应用到复选框。

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

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

          【讨论】:

            猜你喜欢
            • 2013-02-19
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2015-11-17
            • 2012-08-08
            • 1970-01-01
            • 2012-07-01
            • 1970-01-01
            相关资源
            最近更新 更多