代码之家  ›  专栏  ›  技术社区  ›  filo

Android style.xml-如何更改复选框标签文本颜色?[副本]

  •  0
  • filo  · 技术社区  · 6 年前

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

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

    enter image description here

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

    <item name="android:checkboxStyle">@style/App_CheckboxStyle</item>
    
    0 回复  |  直到 7 年前
        1
  •  16
  •   Helen    7 年前

    我知道这个问题已经问了很久了,但我想补充一下我的解决方案。它适用于所有的SDK版本。

    您是对的,要更改整个项目中的复选框样式,必须将该行添加到主题中:

    <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,则不需要使用支持库,只需使用 安卓:按钮

        2
  •  4
  •   Community CDub    7 年前

    你可以改变 checkbox ,使用此:

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

    你可以改变 textView 复选框 ,使用此:

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

    xml 如前所述自定义颜色的文件 in this answer (看上面写的 第二种方法

        3
  •  4
  •   Ludo    7 年前

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

    <CheckBox
    android:textColor="@color/mycolor"
    ...
    />
    
        4
  •  4
  •   Linh    7 年前

    你可以用 android:textColorPrimaryDisableOnly 为了改变 CheckBox / RadioButton textColor colorAccent 换成可拉拔的

    <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

        5
  •  1
  •   Aditya Vyas-Lakhan user1796260    9 年前

    你可以用这种可拉的来改变颜色

    <?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
  •  1
  •   Arnav Rao    7 年前

    更改文本颜色所能做的就是定义如下所示的样式,并使用样式属性将其应用于复选框。

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

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