简体   繁体   English

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

[英]How to change check box tick color in android

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.我正在开发 android 应用程序,因为我使用复选框,但默认复选框刻度颜色是蓝色,所以我想将该颜色更改为黄色。 is there any inbuilt property to set color to check box tick.是否有任何内置属性可以将颜色设置为复选框勾选。

You can do this without changing the drawable using buttonTint (as of API 23): 您可以在不使用buttonTint更改drawable的情况下执行此操作(从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" /> 

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.xmlres\\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>

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.png in your drawables folder.drawables文件夹中添加unchecked.pngchecked.png These are checked and unchecked image of checkbox.这些是复选框的选中和未选中的图像。

You can use the attribute app:buttonTint of the AppCompatCheckBox from the android.support.v7 library.您可以使用 android.support.v7 库中AppCompatCheckBox的属性app:buttonTint

<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 以下,您无需重新绘制复选框。

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

android:buttonTint="#FFFF00"

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 的回答给了我尝试这个的想法,因为我不能简单地使用属性设置颜色。 :) :)

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使用这个你可以改变颜色或设置不同的颜色

If you want to change only tint color than must go with the below solution.如果您只想更改色调颜色,则必须使用以下解决方案。 Its work perfectly.它的工作完美。 Create a Selector "check_box_tint.xml" in your res/drawable folder.在 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>

Now Use this drawable as color of your checkbox tint.现在将此可绘制对象用作复选框色调的颜色。

<CheckBox
   android:id="@+id/cbSelectAll"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:buttonTint="@drawable/check_box_tint"/>

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.像这样。

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, eg checkboxStyle and then implement it as a theme: android:theme="@style/checkboxStyle"简而言之:为复选框创建一个样式,例如checkboxStyle然后将其实现为主题: android:theme="@style/checkboxStyle"

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>

For applying color programmatically it will require API Level >19 if your min sdk is >19 then you can use对于以编程方式应用颜色,如果您的最小 sdk > 19,则需要 API 级别 >19,那么您可以使用

checkbox[i]!!.setButtonTintList(getColorStateList(activity!!,R.color.green))

 OR

view.setButtonTintList(getColorStateList(activity!!,R.color.green)) view.setButtonTintList(getColorStateList(activity!!,R.color.green))

科特林版本:

checkBox.buttonTintList = ColorStateList.valueOf(R.color.colorPrimary)

If nothing works than use AppCompatCheckBox with app:buttonCompat="your_drawable_selector"如果没有什么比使用 AppCompatCheckBox 和 app:buttonCompat="your_drawable_selector"

This is working with png.这是与 png 一起使用的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM