Android:设置CheckBox的颜色

我search了一些地方,似乎无法弄清楚CheckBox边框的CheckBox可绘制。 任何人都可以指出我的方向吗?

这是什么看起来像未经检查(几乎看不到的框)

在这里输入图像描述

这是选中的状态

在这里输入图像描述

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

在这里输入图像描述

您可以使用自定义checkboxxml文件。 将下面的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> 

然后使用这个文件作为你的checkbox的背景,像这样:

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

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

未勾选CheckBox选中CheckBox

同样的问题也发生,当您使用主题全息黑暗的活动和白色背景。 所以checkbox有黑暗的风格。 简单的解决方法是从Android的Holo Light直接设置背景:

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

你可以find很好的概述如何所有这些工作在以下答案: https : //stackoverflow.com/a/10139809/1170154

由于Android 5和API级别21,可以自由selectcheckbox的颜色(和许多其他小部件)。 将以下内容添加到values-v21/styles.xml (同时确保在values/styles.xml有早期API的回退:

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

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

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

就是这样,checkbox以您喜欢的颜色显示!

好吧,所以我很抱歉,但是这些答案大部分都是不完整的,或者有一些小问题。 不同版本的Android中的“样式”控件是一个史诗般的痛苦。 在一个非常严格的devise限制的项目中,我把头发拉了几天,终于打破了,写了一个testing应用程序,然后真正挖掘和testing各种解决scheme外观造型开关和checkbox,因为当一个devise有一个它经常有另一个。 这是我发现的…

首先:你实际上不能对其中任何一个样式进行devise,但是你可以将一个主题应用到所有的主题上,或者只是其中的一个。

第二:你可以用XML完成所有的工作,而且你不需要第二个值-v21 / styles.xml。

第三:说到开关,如果你想支持Android的老版本,就有两个基本select(就像我敢肯定的那样)。

  1. 你可以使用一个SwitchCompat并且你将能够在不同的平台上看起来相同。
  2. 你可以使用一个Switch ,你将能够用你的主题的其余部分,或只是特定的开关,在老版本的Android的主题,你只会看到一个没有风格的旧方形开关。

好吧,现在简单的参考代码。 再次,如果你创build一个简单的Hello World! 并放弃这个代码,你可以发挥你的心中的内容。 所有这一切都是锅炉板,所以我只是要包括活动和风格的XML …

activity_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 …

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

我知道,我知道,你懒得build立这个,你只是想写你的代码。 我知道了。 这是它运行时的样子

API_21:

API 21

API_18:

API18

它由drawable指定: android.R.drawable.checkbox_off_backgroundandroid.R.drawable.checkbox_on_background

您可以像这个API21&以上设置CHECKBOX颜色

机器人:buttonTint = “@颜色/ YOUR_COLOR”

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

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

应用程式:buttonTint = “@颜色/ YOUR_COLOR”

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

这将是最有效的方式。

机器人:buttonTint = “@彩色/黑白”