Android的CheckBox的更改默认颜色复选标记(Changing Default Color

2019-06-26 00:10发布

如何更改从绿色选中标记为蓝色了Android复选框的默认颜色为特定的复选框?

Answer 1:

不幸的是,改变颜色不是一个简单的属性。 复选标记是图像,所以你必须创建一个自定义图像。 看看这个例子

创建一个选择的XML文件如这样:

<?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/star_down" />
    <item android:state_checked="false" android:drawable="@drawable/star" />
</selector>

保存在该XML文件res\drawables\文件夹中。 然后,你的布局文件中把它应用到您的复选框是这样的:

<CheckBox
    android:text="Custom CheckBox"
    android:button="@drawable/checkbox_selector"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

在这个例子中你命名你选择XML文件“checkbox_selector.xml”和你需要一个star_down.png,并star.png在绘图资源文件夹以及。 您可以使用此技术通过改变系统复选框图像,任何你想要的颜色和选择引用改变PNG文件,以创建不同颜色的复选框。



Answer 2:

这是很容易使用XML做buttonTint (如API等级23):

<CheckBox
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:buttonTint="@color/COLOR_HERE" />

和尼古拉斯指出的那样,你可以做到这一点使用appCompatCheckbox v7对老年人的API:

<android.support.v7.widget.AppCompatCheckBox 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    app:buttonTint="@color/COLOR_HERE" /> 


文章来源: Changing Default Color of Android CheckBox Check Mark