Android:更改复选框颜色(boxbackground和对勾标记颜色)

时间:2020-04-13 09:04:13

标签: android user-interface checkbox styles

我正在使用以下复选框

checkbox img

我的代码...

NULL

style.xml

   <CheckBox
    android:id="@+id/checkBox10"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="32dp"
    android:layout_weight="1"
    android:button="@null"
    android:checked="true"
    android:drawableBottom="?android:attr/listChoiceIndicatorMultiple"
    android:gravity="center_horizontal"
    android:text="10"
    android:textColor="#FFFFFF"
    android:textStyle="bold"
    android:theme="@style/MyCheckBox"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

我可以通过更改样式xml中的颜色来更改框的颜色。就像我在使用

 <style name="MyCheckBox" parent="Theme.AppCompat.Light">
    <item name="colorControlNormal">#ffffff</item>
    <item name="colorControlActivated">#ff0000</item>       
 </style>

我找不到改变刻度颜色的方法。 我需要更改刻度颜色(蓝色)作为背景颜色。 我该怎么办??

1 个答案:

答案 0 :(得分:2)

res\drawables\文件夹中创建名称为cb_selector。xml的选择器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/checked" />
    <item android:state_checked="false" android:drawable="@drawable/unchecked" />
</selector>

在您的布局文件中将此文件应用到您的复选框

<CheckBox
    android:id="@+id/cb"
    android:text="My CheckBox"
    android:button="@drawable/cb_selector"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

在可绘制文件夹中添加unchecked.pngchecked.png。这些是复选框的选中和未选中图像。

您也可以使用color代替drawable