我如何改变默认的复选框的颜色在Android? 默认情况下,复选框的颜色是绿色,我想改变这个颜色。 如果不可能,请告诉我如何做一个自定义复选框?
当前回答
使用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 /颜色/ 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>
其他回答
如果你的minSdkVersion是21+使用android:buttonTint属性来更新复选框的颜色:
<CheckBox
...
android:buttonTint="@color/tint_color" />
在使用AppCompat库和支持低于21的Android版本的项目中,你可以使用buttonTint属性的compat版本:
<CheckBox
...
app:buttonTint="@color/tint_color" />
在这种情况下,如果你想子类化一个CheckBox,不要忘记使用AppCompatCheckBox代替。
之前的回答:
你可以使用android:button="@drawable/your_check_drawable"属性来改变checkboxes的可绘制性。
我的目标是使用自定义复选框按钮并删除强调色按钮色调。我尝试了公认的答案,但现在却起作用了,这对我来说很管用:
在styles.xml文件中
<style name="Theme.Checkbox" parent="Widget.AppCompat.CompoundButton.CheckBox">
<item name="android:button">@drawable/theme_checkbox_button</item>
<item name="android:drawableLeft">@android:color/transparent</item>
<item name="android:drawablePadding">10dp</item>
<item name="android:buttonTint">@android:color/transparent</item>
<item name="android:buttonTintMode">screen</item>
</style>
现在对于这个问题,唯一需要的属性是android:buttonTint和android:buttonTintMode
下面是一些额外的内容:
我需要使用一个自定义drawable,因此android:button设置了一个选择器drawable 我需要一些空格之间的盒子和文本,因此我遵循这个SO答案,并设置android: drawablleft和android:drawablePadding
我的minSdkVersion是15,我的BaseAppTheme父是Theme.AppCompat.Light.NoActionBar,我正在以编程方式创建我的复选框。下面的步骤对我很有效。
1. 在Java代码中进行更改
CheckBox checkBox = new CheckBox(context);
to
AppCompatCheckBox checkBox = new AppCompatCheckBox(context);
2. 在你的styles.xml中,添加:
<style name="MyCheckboxStyle" parent="Widget.AppCompat.CompoundButton.CheckBox">
<item name="buttonTint">@color/primary_dark</item>
</style>
3.最后,在你的BaseAppTheme(或AppTheme)样式中,添加:
<item name="checkboxStyle">@style/MyCheckboxStyle</item>
<item name="android:checkboxStyle">@style/MyCheckboxStyle</item>
您可以通过将<CheckBox>嵌入到<LinearLayout>中来更改它的背景颜色。然后将<LinearLayout>的背景色更改为您想要的颜色。
我更喜欢我创建的这个扩展函数:
fun CheckBox.setTint(@ColorInt color: Int?) {
if (color == null) {
CompoundButtonCompat.setButtonTintList(this, null)
return
}
CompoundButtonCompat.setButtonTintMode(this, Mode.SRC_ATOP)
CompoundButtonCompat.setButtonTintList(this, ColorStateList.valueOf(color))
}
它非常类似于我的ImageView,以防有人也想要它:
fun ImageView.setTint(@ColorInt color: Int?) {
if (color == null) {
ImageViewCompat.setImageTintList(this, null)
return
}
ImageViewCompat.setImageTintMode(this, Mode.SRC_ATOP)
ImageViewCompat.setImageTintList(this, ColorStateList.valueOf(color))
}
推荐文章
- 如何解决INSTALL_FAILED_DEXOPT错误?
- 有没有办法自动安装Android SDK ?
- 如何检查一个视图在Android中是否可见?
- Android是否保留。apk文件?如果有,在哪里?
- 在Android 5棒棒糖中,通知栏图标变成白色
- 从URI获取真实路径,Android奇巧新的存储访问框架
- 如何检查JSON键是否存在?
- ImageView -有高度匹配宽度?
- 如何确定在android文件的MIME类型?
- 这是在Android中获取用户位置的好方法
- Android从左到右幻灯片动画
- 如何检索视图的维度?
- 如何改变菜单项的文本颜色在安卓?
- Android选择器和文本颜色
- 视图绑定-我如何获得包含布局的绑定?