我如何改变默认的复选框的颜色在Android? 默认情况下,复选框的颜色是绿色,我想改变这个颜色。 如果不可能,请告诉我如何做一个自定义复选框?
当前回答
通过设置ColorStateList,有一种更简单的方法以编程方式设置颜色。
Checkbox.setButtonTintList(ColorStateList.valueOf(getContext().getColor(R.color.yourcolor)))
其他回答
我的目标是使用自定义复选框按钮并删除强调色按钮色调。我尝试了公认的答案,但现在却起作用了,这对我来说很管用:
在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
通过设置ColorStateList,有一种更简单的方法以编程方式设置颜色。
Checkbox.setButtonTintList(ColorStateList.valueOf(getContext().getColor(R.color.yourcolor)))
你可以设置复选框的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下工作
您可以在“colors.xml”中使用以下两个属性
<color name="colorControlNormal">#eeeeee</color>
<color name="colorControlActivated">#eeeeee</color>
colorControlNormal用于复选框的正常视图,colorControlActivated用于复选框被选中时的视图。
您可以使用下面的代码行在java代码中动态更改复选框的背景。
//Setting up background color on checkbox.
checkbox.setBackgroundColor(Color.parseColor("#00e2a5"));
这个答案来自这个网站。你也可以使用这个网站将你的RGB颜色转换为十六进制值,你需要提供parseColor
推荐文章
- Android:为什么视图没有maxHeight ?
- 如何获得具有已知资源名称的资源id ?
- 在Android上将字符串转换为整数
- 为什么“System.out。”println“工作在Android?
- WebView显示err_cleartext_not_allowed尽管站点是HTTPS
- Android M权限:对shouldShowRequestPermissionRationale()函数的使用感到困惑
- 为什么构建类型与产品风味不同?
- 我如何在一个片段中访问getSupportFragmentManager() ?
- 调试在哪里。Android Studio中的密钥存储库
- 从资产中读取文件
- 在不活动的地方调用getLayoutInflater()
- 设置Android布局元素的背景颜色
- 错误:'keytool'不能被识别为内部或外部命令、可操作程序或批处理文件
- 在应用程序本身中更改Locale
- apk (.apk)和应用程序包(.aab)的区别