我该如何将Android默认复选框的绿色勾选标记更改为特定CheckBox的蓝色?
我该如何将Android默认复选框的绿色勾选标记更改为特定CheckBox的蓝色?
很遗憾,更改颜色并不是一个简单的属性。选中标记是一个图像,因此您必须创建自定义图像。请查看这个示例
创建一个选择器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"/>
在这个例子中,您需要将选择器文件命名为"checkbox_selector.xml",并且您还需要在drawable文件夹中拥有一个star_down.png和star.png。您可以使用这种技术通过将系统复选框图像更改为所需颜色来创建不同颜色的复选框,并在选择器中引用修改后的png文件。如果使用xml,使用buttonTint
很容易实现(API级别23及以上):
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:buttonTint="@color/COLOR_HERE" />
正如Nicolás所指出的,您可以使用appCompatCheckbox v7
来处理旧版本API:
<android.support.v7.widget.AppCompatCheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:buttonTint="@color/COLOR_HERE" />
<android.support.v7.widget.AppCompatCheckBox android:layout_width="wrap_content" android:layout_height="wrap_content" app:buttonTint="@color/COLOR_HERE" />
从API级别7开始实现此操作。 - Nicolás Carrasco-Stevenson