设置未选中复选框Android的背景颜色

Jim*_*nts 5 checkbox android android-checkbox

它必须是这样的:

在此输入图像描述

目前它看起来像这样:

在此输入图像描述

圆角矩形并不重要.但是未经检查的状态必须是黑色的.这是我的代码:

<CheckBox
    android:id="@+id/checkbox"
    android:buttonTint="@color/orange"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>
Run Code Online (Sandbox Code Playgroud)

这是我正在使用的主题:

  "Theme.AppCompat.Light.NoActionBar"
Run Code Online (Sandbox Code Playgroud)

我也试过这个:

<style name="checkbox_style" parent="Bijenkorf">
    <item name="material_grey_600">@color/black</item>
</style>
Run Code Online (Sandbox Code Playgroud)

因为未选中的默认值为#757575.我在主题中看了这个,它是"material_grey_600".但这不编译.有任何想法吗?

D_A*_*pha 16

像这样做-

在您的布局文件中 -

<android.support.v7.widget.AppCompatCheckBox
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:theme="@style/CheckboxStyle"/>
Run Code Online (Sandbox Code Playgroud)

并在您的style.xml文件中

<style name="CheckboxStyle" parent="AppTheme">
    <item name="colorAccent">@color/colorAccent</item>
</style>
Run Code Online (Sandbox Code Playgroud)

或者您可以更改已选中和未选中的颜色,如下所示 -

<style name="CheckboxStyle" parent="AppTheme">
    <item name="colorAccent">@color/colorAccent</item>  // for checked 
    <item name="android:textColorSecondary">@color/colorSecondary</item>  // for unchecked
</style>
Run Code Online (Sandbox Code Playgroud)

用您需要的颜色更改colorAccent和colorSecondary.

希望它会帮助你..


Ram*_*ani 5

我知道已经回答了,但是有人可能会发现这很有用,另一种使用颜色状态列表实现此目的的方法,是在xml文件中为复选框声明颜色状态列表

checkbox_state_list.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
   <item android:color="@color/orange" android:state_checked="true"/>
   <item android:color="@color/black" android:state_checked="false"/>
</selector>
Run Code Online (Sandbox Code Playgroud)

现在将此状态列表用作

<android.support.v7.widget.AppCompatCheckBox
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:buttonTint="@color/checkbox_state_list"/>
Run Code Online (Sandbox Code Playgroud)