我正在开发Android应用程序,因为我使用复选框,但默认复选框勾选颜色是蓝色,所以我想将该颜色更改为黄色。是否有任何内置属性可以设置复选框勾选的颜色。
不幸的是,更改复选框复选标记的颜色并不是一个简单的属性
在
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.png
文件夹中添加 checked.png
和 drawables
。这些是选中和未选中的复选框图像。
您可以使用 android.support.v7 库中
app:buttonTint
的属性 AppCompatCheckBox
。
<android.support.v7.widget.AppCompatCheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:buttonTint="@color/colorAccent"/>
优点:也可以在 API 21 以下使用,并且不需要重新绘制复选框。
从 API 21 开始,您可以使用按钮色调属性
android:buttonTint="#FFFF00"
如果您想以编程方式执行此操作,则只需执行以下操作:
final CheckBox cb = new CheckBox(getApplicationContext());
cb.setButtonTintList(getColorStateList(R.color.colorAccent));
Chris Stillwell 的回答给了我尝试这个的想法,因为我不能简单地使用属性设置颜色。 :)
如果您只想更改色调,则必须使用以下解决方案。它的工作完美。 在 res/drawable 文件夹中创建一个选择器“check_box_tint.xml”。
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:color="@color/your_checked_color" />
<item android:state_checked="false" android:color="@color/your_unchecked_color" />
</selector>
现在使用此可绘制对象作为复选框色调的颜色。
<CheckBox
android:id="@+id/cbSelectAll"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:buttonTint="@drawable/check_box_tint"/>
转到 styles.xml 并添加此行。
<style>
<item name="colorAccent">@android:color/holo_green_dark</item>
</style>
使用它您可以更改颜色或设置不同的颜色
首先,我们必须创建一个包含选中和取消选中颜色情况的drawable,然后你必须将这个drawable设置为buttonTint;
可绘制_复选框;
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:color="@color/kelleyGreen" />
<item android:state_checked="false" android:color="@color/warmGrey" />
</selector>
<style name="CheckBox" parent="Widget.AppCompat.CompoundButton.CheckBox">
<item name="android:textAppearance">@style/TextAppearance.Regular.XSmall</item>
<item name="android:textColor">@color/warmGrey</item>
<item name="buttonTint">@drawable/drawable_checkbox</item>
</style>
Kotlin 版本:
checkBox.buttonTintList = ColorStateList.valueOf(R.color.colorPrimary)
对复选框使用自定义选择器。
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/patch_pressed" android:state_pressed="true"/>
<item android:drawable="@drawable/patch_normal" android:state_enabled="true"/>
<item android:drawable="@drawable/patchdisable" android:state_enabled="false"/>
</selector>
像这样。
对于那些仍在寻找答案的人(我知道这是一个较旧的问题)——我发现这个解决方案效果很好,无需担心 API:https://stackoverflow.com/a/31840734/7601437
简而言之:为复选框创建一个样式,例如
checkboxStyle
,然后将其实现为主题:android:theme="@style/checkboxStyle"
以编程方式应用颜色,如果您的最小 sdk >19,则需要 API 级别 >19,那么您可以使用
checkbox[i]!!.setButtonTintList(getColorStateList(activity!!,R.color.green))
OR
view.setButtonTintList(getColorStateList(activity!!,R.color.green))
如果没有任何效果,请将 AppCompatCheckBox 与 app:buttonCompat="your_drawable_selector" 一起使用
这适用于 png。
对我来说,这对于选中和未选中的复选框颜色变化非常有效
val states = arrayOf(
intArrayOf(-android.R.attr.state_checked),
intArrayOf(android.R.attr.state_checked)
)
val colors = intArrayOf(
requireContext().getColor(R.color.unchecked_color),
requireContext().getColor(R.color.checked_color)
)
val colorStateList = ColorStateList(states, colors)
checkbox.buttonTintList = colorStateList