如何在android中更改复选框勾选颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20542693/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 03:00:50  来源:igfitidea点击:

How to change check box tick color in android

androidcheckbox

提问by Atul K

I am developing android application In that i use check box but default check box tick color is blue so i want to change that color to yellow. is there any inbuilt property to set color to check box tick.

我正在开发 android 应用程序,因为我使用复选框,但默认复选框刻度颜色是蓝色,所以我想将该颜色更改为黄色。是否有任何内置属性可以将颜色设置为复选框勾选。

回答by afathman

You can do this without changing the drawable using buttonTint (as of API 23):

您可以使用 buttonTint(从 API 23 开始)在不更改可绘制对象的情况下执行此操作:

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

or use AppCombatCheckBox for older versions of android/compatibility.

或将 AppCombatCheckBox 用于旧版本的 android/兼容性。

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

回答by Vaibhav Agarwal

Unfortunately, changing the color of checkbox check mark isn't a simple attribute

不幸的是,更改复选框复选标记的颜色不是一个简单的属性

Create a selector xml file in res\drawables\folder with name cb_selector.xml

res\drawables\具有名称的文件夹中创建一个选择器 xml 文件cb_selector.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>

In your layout file apply this file to your checkBox

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

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

Add a unchecked.png, and checked.pngin your drawablesfolder. These are checked and unchecked image of checkbox.

在您的文件夹中添加一个unchecked.png, 和。这些是复选框的选中和未选中的图像。checked.pngdrawables

回答by dabo248

You can use the attribute app:buttonTintof the AppCompatCheckBoxfrom the android.support.v7 library.

您可以使用属性app:buttonTintAppCompatCheckBox从android.support.v7库。

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

Advantage: works also below API 21 and you don't need to redraw the checkbox.

优点:也适用于 API 21 以下,您无需重新绘制复选框。

回答by Chris Stillwell

As of API 21 you can use the Button Tint attribute

从 API 21 开始,您可以使用 Button Tint 属性

android:buttonTint="#FFFF00"

回答by Yogesh Rathi

Go to styles.xml and add this line.

转到styles.xml 并添加这一行。

<style>
<item name="colorAccent">@android:color/holo_green_dark</item> 
</style>

using this you can change color or set different color

使用这个你可以改变颜色或设置不同的颜色

回答by Carl Du Plessis

If you want to do this programmatically, then you simply do this:

如果您想以编程方式执行此操作,则只需执行以下操作:

final CheckBox cb = new CheckBox(getApplicationContext());
cb.setButtonTintList(getColorStateList(R.color.colorAccent));

Chris Stillwell's answer gave me the idea to try this as I couldn't simply set the colour using the attributes. :)

Chris Stillwell 的回答给了我尝试这个的想法,因为我不能简单地使用属性设置颜色。:)

回答by ?zer ?zcan

Firstly, we must create a drawable that include checked and uncheck color situations, then you must set this drawable as buttonTint;

首先,我们必须创建一个包含选中和取消选中颜况的可绘制对象,然后您必须将此可绘制对象设置为 buttonTint;

drawable_checkbox;

drawable_checkbox;

<?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>

回答by atschpe

For those still looking for an answer (I am aware this is an older question) – I found this solution works well without having to worry about API: https://stackoverflow.com/a/31840734/7601437

对于那些仍在寻找答案的人(我知道这是一个较旧的问题)– 我发现此解决方案运行良好而无需担心 API:https: //stackoverflow.com/a/31840734/7601437

In short: create a style for the checkbox, e.g. checkboxStyleand then implement it as a theme: android:theme="@style/checkboxStyle"

简而言之:为复选框创建一个样式,例如checkboxStyle,然后将其实现为主题:android:theme="@style/checkboxStyle"

回答by Ashwin S Ashok

Use Custom selector for the checkbox.

为复选框使用自定义选择器。

<?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>

Like this.

像这样。