如何在Android中更改默认的CheckBox颜色?
默认情况下,CheckBox的颜色是绿色的,我想要改变这个颜色。
如果不可能的话,请告诉我如何制作自定义的CheckBox?
buttonTint
来设置框的颜色:(API级别23及以上)<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:buttonTint="@color/CHECK_COLOR" />
您也可以在较旧的API级别中使用appCompatCheckbox v7
来完成此操作:
<android.support.v7.widget.AppCompatCheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:buttonTint="@color/COLOR_HERE" />
如果您的minSdkVersion
是21+,请使用android:buttonTint
属性来更新复选框的颜色:
<CheckBox
...
android:buttonTint="@color/tint_color" />
在使用AppCompat库并支持安卓21版本以下的项目中,你可以使用buttonTint
属性的兼容版本:
<CheckBox
...
app:buttonTint="@color/tint_color" />
在这种情况下,如果您想子类化 CheckBox
,不要忘记使用 AppCompatCheckBox
代替。android:button="@drawable/your_check_drawable"
属性的形式可以更改 CheckBox
的可绘制部分。contentControl
选项可供使用:https://materialdoc.com/components/selection-controls/ - SimpsOffbuttonTint
的值似乎会覆盖textColor
的值。有什么解决方法吗? - Alex Semeniuk您可以在styles.xml中设置Android复选框的主题,以获取所需的颜色,添加:
<style name="checkBoxStyle" parent="Base.Theme.AppCompat">
<item name="colorAccent">CHECKEDHIGHLIGHTCOLOR</item>
<item name="android:textColorSecondary">UNCHECKEDCOLOR</item>
</style>
然后在您的布局文件中:
<CheckBox
android:theme="@style/checkBoxStyle"
android:id="@+id/chooseItemCheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
与使用 android:buttonTint="@color/CHECK_COLOR"
不同,这种方法适用于 Api 23 及以下版本
buttonTint
可更改按钮和21+ api版本以上的颜色选择器的颜色。<android.support.v7.widget.AppCompatCheckBox
android:id="@+id/check"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:buttonTint="@color/checkbox_filter_tint"
tools:targetApi="21"/>
res/colors/checkbox_filter_tint.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/light_gray_checkbox"
android:state_checked="false"/>
<item android:color="@color/common_red"
android:state_checked="true"/>
</selector>
编程版本:
int [][] states = {{android.R.attr.state_checked}, {}};
int [] colors = {color_for_state_checked, color_for_state_normal}
CompoundButtonCompat.setButtonTintList(checkbox, new ColorStateList(states, colors));
我建议在Android中使用样式方式来配置内置的Android视图,并在项目中添加新的样式:
<style name="yourStyle" parent="Base.Theme.AppCompat">
<item name="colorAccent">your_color</item> <!-- for uncheck state -->
<item name="android:textColorSecondary">your color</item> <!-- for check state -->
</style>
并将此样式分配给复选框的主题: android:theme="@style/youStyle"
希望这可以帮助到您。
<CheckBox
android:id="@+id/chk_remember_signup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:buttonTint="@android:color/white"
android:text="@string/hint_chk_remember_me" />
styles.xml
文件中添加这一行:<style>
<item name="android:colorAccent">@android:color/holo_green_dark</item>
</style>
我曾经遇到过同样的问题,我使用以下技巧找到了一个解决方案。从android-sdk/platforms/android-#(version)/data/res/drawable
中复制btn_check.xml
到您项目的可绘制文件夹中,并将“on”和“off”图像状态更改为您自定义的图像。
然后您的xml只需要android:button="@drawable/btn_check"
<CheckBox
android:button="@drawable/btn_check"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true" />
android:button="@android:drawable/..."
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:id="@+id/agreeCheckBox"
android:text="@string/i_agree_to_terms_s"
android:buttonTint="@color/white"
android:layout_below="@+id/avoid_spam_text"/>
android:buttonTint="@color/mybrown"
进行设置是改变方框颜色的简单方法。 - shauvikapp:buttonTint="@color/mybrown"
来代替,这样可以在API < 21上工作。 - Nikaoto