Android:设置复选框的颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11464143/
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 07:44:54  来源:igfitidea点击:

Android: Set color of CheckBox

androidandroid-layout

提问by Kirk

I've searched a number of places and don't seem to be able to figure out the CheckBoxdrawable for the border of the check box. Can anyone point me in the correct direction?

我搜索了很多地方,似乎无法找出CheckBox复选框边框的可绘制对象。任何人都可以指出我正确的方向吗?

Here is what it looks like unchecked (Can barely see the box)

这是未选中的样子(几乎看不到框)

enter image description here

在此处输入图片说明

Here is the checked state

这是选中的状态

enter image description here

在此处输入图片说明

Here is what I'm trying to make it look like.

这就是我试图让它看起来像的。

enter image description here

在此处输入图片说明

回答by Ram kiran

You can use a custom check box xml file for this. Save the below xml code in drawablesfolder, name it custom_checkbox.xml:

您可以为此使用自定义复选框 xml 文件。将以下 xml 代码保存在drawables文件夹中,命名为custom_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/cbchk_blue"
        android:state_focused="false">
    </item>
    <item android:state_checked="true" 
        android:drawable="@drawable/cbchk_blue"
        android:state_focused="true">
    </item>
    <item android:state_checked="false" 
        android:drawable="@drawable/cbunchk_blue"
        android:state_focused="false">
    </item>
    <item android:state_checked="false" 
        android:drawable="@drawable/cbunchk_blue"
        android:state_focused="true">
    </item>
</selector>

Then use this file as background of your checkbox like this:

然后将此文件用作复选框的背景,如下所示:

       <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:button="@drawable/custom_checkbox"
        android:id="@+id/checkBox" />

Here I am uploading my own images which I used in place of cbchk_blueand cbunchk_blue

在这里,我上传了我自己的图片,我用它代替了cbchk_bluecbunchk_blue

Unchecked CheckBoxChecked CheckBox

未选中的复选框选中复选框

回答by Yuriy Ashaev

Same problem also happens when you use theme Holo Dark for Activity and white background. So checkbox has Dark style. Simple workaround is directly set background from Android's Holo Light:

当您将主题 Holo Dark 用于活动和白色背景时,也会发生同样的问题。所以复选框具有深色样式。简单的解决方法是直接从 Android 的 Holo Light 设置背景:

int id = Resources.getSystem().getIdentifier("btn_check_holo_light", "drawable", "android");
checkBox.setButtonDrawable(id);

You can find great overview how all this things work in following answer: https://stackoverflow.com/a/10139809/1170154

您可以在以下答案中找到所有这些事情如何工作的很好的概述:https: //stackoverflow.com/a/10139809/1170154

回答by Phoca

Since Android 5 and API level 21 it is possible to freely choose colors of checkboxes (and many other widgets). Add the following to your values-v21/styles.xml(while making sure you have a fallback for earlier APIs in values/styles.xml:

从 Android 5 和 API 级别 21 开始,可以自由选择复选框(和许多其他小部件)的颜色。将以下内容添加到您的values-v21/styles.xml(同时确保您对 中的早期 API 有回退values/styles.xml

<style name="CustomCheckBox">
    <item name="android:theme">@style/CheckBoxAppTheme</item>
</style>

<style name="CheckBoxAppTheme">
    <item name="android:colorAccent">
        @color/theFillColorInCheckedState
    </item>
    <item name="android:colorControlNormal">
        @color/theBorderColorInUncheckedState
    </item>
    <item name="android:colorControlHighlight">
        @color/theBackgroundColorWhenFocusingTheCheckBox
    </item>
</style>

Then you just have to apply the style to your checkbox in your layout:

然后,您只需将样式应用于布局中的复选框:

<CheckBox
    style="@style/CustomCheckBox" />

That's it, the checkboxes appear in your favourite colours!

就是这样,复选框以您喜欢的颜色出现!

回答by Aaron Cooley

Ok, so I'm sorry but most of these answers are incomplete or have some minor bug in them. 'Styling' controls across different versions of Android is an epicpain in the ass. After pulling my hair out for days on a project with very tight design constraints I finally broke down and wrote a test app and then really dug in and tested the various solutions out there for styling switches and check-boxes, since when a design has one it frequently has the other. Here's what I found...

好的,我很抱歉,但这些答案中的大多数都不完整或存在一些小错误。“样式”在不同的Android版本控制是一个史诗般的屁股痛。在一个设计约束非常严格的项目上拉了几天头发之后,我终于崩溃了并编写了一个测试应用程序,然后真正挖掘并测试了各种样式开关和复选框的解决方案,因为当一个设计有一个它经常有另一个。这是我发现的...

First:You can't actually style either of them, but you can apply a theme to all of them, or just one of them.

第一:您实际上无法为它们中的任何一个设置样式,但您可以将主题应用于所有它们,或仅应用于其中一个。

Second:You can do it all from XML and you don't need a second values-v21/styles.xml.

第二:您可以从 XML 中完成所有操作,并且不需要第二个 values-v21/styles.xml。

Third:when it comes to switches you have two basic choices if you want to support older versions of Android (like I'm sure you do)...

第三:谈到开关,如果你想支持旧版本的 Android,你有两个基本选择(就像我确定你这样做)......

  1. You can use a SwitchCompatand you will be able to make it look the same across platforms.
  2. You can use a Switchand you will be able to theme it with the rest of your theme, or just that particular switch and on older versions of Android you'll just see an unstyled older square switch.
  1. 您可以使用 aSwitchCompat并且您将能够使其跨平台看起来相同。
  2. 您可以使用 aSwitch并且您将能够将其与您的主题的其余部分一起设置主题,或者仅使用那个特定的开关,而在旧版本的 Android 上,您只会看到一个无样式的旧方形开关。

Ok now for the simple reference code. Again if you create a simple Hello World! and drop this code in you can play to your hearts content. All of that is boiler plate here so I'm just going to include the XML for the activity and the style...

好的,现在是简单的参考代码。同样,如果您创建一个简单的 Hello World!并将此代码放入您可以尽情玩乐的内容。所有这些都是样板文件,所以我将包括活动和样式的 XML...

activity_main.xml...

活动_main.xml...

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.kunai.switchtest.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="'Styled' SwitchCompat" />

    <android.support.v7.widget.SwitchCompat
        android:id="@+id/switch_item"
        android:layout_width="wrap_content"
        android:layout_height="46dp"
        android:layout_alignParentEnd="true"
        android:layout_marginEnd="16dp"
        android:checked="true"
        android:longClickable="false"
        android:textOff="OFF"
        android:textOn="ON"
        app:switchTextAppearance="@style/BrandedSwitch.text"
        app:theme="@style/BrandedSwitch.control"
        app:showText="true" />

</RelativeLayout>

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.kunai.switchtest.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Themed SwitchCompat" />

    <android.support.v7.widget.SwitchCompat
        android:id="@+id/switch_item2"
        android:layout_width="wrap_content"
        android:layout_height="46dp"
        android:layout_alignParentEnd="true"
        android:layout_marginEnd="16dp"
        android:checked="true"
        android:longClickable="false" />

</RelativeLayout>

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.kunai.switchtest.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Themed Switch" />

    <Switch
        android:id="@+id/switch_item3"
        android:layout_width="wrap_content"
        android:layout_height="46dp"
        android:layout_alignParentEnd="true"
        android:layout_marginEnd="16dp"
        android:checked="true"
        android:longClickable="false"
        android:textOff="OFF"
        android:textOn="ON"/>

</RelativeLayout>

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.kunai.switchtest.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="'Styled' Switch" />

    <Switch
        android:id="@+id/switch_item4"
        android:layout_width="wrap_content"
        android:layout_height="46dp"
        android:layout_alignParentEnd="true"
        android:layout_marginEnd="16dp"
        android:checked="true"
        android:longClickable="false"
        android:textOff="OFF"
        android:textOn="ON"
        android:theme="@style/BrandedSwitch"/>

</RelativeLayout>

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.kunai.switchtest.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="'Styled' CheckBox" />

    <CheckBox
        android:id="@+id/checkbox"
        android:layout_width="wrap_content"
        android:layout_height="46dp"
        android:layout_alignParentEnd="true"
        android:layout_marginEnd="16dp"
        android:checked="true"
        android:longClickable="false"
        android:theme="@style/BrandedCheckBox"/>

</RelativeLayout>

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.kunai.switchtest.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Themed CheckBox" />

    <CheckBox
        android:id="@+id/checkbox2"
        android:layout_width="wrap_content"
        android:layout_height="46dp"
        android:layout_alignParentEnd="true"
        android:layout_marginEnd="16dp"
        android:checked="true"
        android:longClickable="false"/>

</RelativeLayout>

styles.xml...

样式.xml...

<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">#3F51B5</item>
    <item name="colorPrimaryDark">#303F9F</item>
    <item name="colorAccent">#FF4081</item>
</style>

<style name="BrandedSwitch.control" parent="Theme.AppCompat.Light">
    <!-- active thumb & track color (30% transparency) -->
    <item name="colorControlActivated">#e6e600</item>
    <item name="colorSwitchThumbNormal">#cc0000</item>
</style>

<style name="BrandedSwitch.text" parent="Theme.AppCompat.Light">
    <item name="android:textColor">#ffa000</item>
    <item name="android:textSize">9dp</item>
</style>

<style name="BrandedCheckBox" parent="AppTheme">
    <item name="colorAccent">#aaf000</item>
    <item name="colorControlNormal">#ff0000</item>
</style>

<style name="BrandedSwitch" parent="AppTheme">
    <item name="colorAccent">#39ac39</item>
</style>

I know, I know, you are too lazy to build this, you just want to get your code written. I get it. Here's what it looks like when you run it...

我知道,我知道,你懒得构建这个,你只想写你的代码。我得到它。这是运行时的样子...

API_21:

API_21:

API 21

API 21

API_18:

API_18:

API18

API18

回答by Keyur

You can set the CHECKBOX colorlike this API21 & Above

您可以像API21 及以上一样设置CHECKBOX 颜色

android:buttonTint="@color/YOUR_COLOR"

android:buttonTint="@color/YOUR_COLOR"

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

For the support of Older versionuse AppCompatCheckBoxof V7 library

对于旧版本支持使用V7 库的AppCompatCheckBox

app:buttonTint="@color/YOUR_COLOR"

应用程序:buttonTint="@color/YOUR_COLOR"

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

回答by Neshan Manilka

This will be the most efficient way.

这将是最有效的方式。

android:buttonTint="@color/black"

android:buttonTint="@color/black"