欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

checkbox自定义样式

程序员文章站 2024-03-26 11:24:17
...

checkbox自定义样式


常见的自定义样式是改变checkbox的button图片,但是他的需求是去掉checkbox的选项框,使checkbox的样式类似button。我最初给的建议是使用TextView,点击一次改变背景颜色,然后保存当前状态(选中或未选中),但还是很麻烦,查找资料,找到了第二种checkbox的自定义样式方式,总结一下,备忘。

一、修改checkbox选项框样式

首先我们要找到两张checkbox选项框的图片:

normal.png

checked.png

然后我们设置一个背景选择器checkbox_style.xml:

<?xml version="1.0" encoding="utf-8"?>
<item android:drawable="@drawable/checked" android:state_checked="true"/>
<item android:drawable="@drawable/normal" android:state_checked="false"/>
<item android:drawable="@drawable/normal"/>

到这里,在往下有两种方案,一种是直接在布局文件的android:button属性中设置:

1
2 <CheckBox
3 android:id="@+id/checkbox1"
4 android:layout_width=“wrap_content”
5 android:layout_height=“wrap_content”
6 android:text="@strings/check_text"
7 android:button="@drawable/checkbox_style"
8 android:checked=“true”/>

还有一种是在style.xml文件中添加样式MyCheckboxStyle,并在布局文件中的style属性中设置:

1
1 <CheckBox
2 android:id="@+id/checkbox1"
3 android:layout_width=“wrap_content”
4 android:layout_height=“wrap_content”
5 style="@style/MyCheckboxStyle" />

作者:myf0908
来源:CSDN
原文:https://blog.csdn.net/myf0908/article/details/76502009
版权声明:本文为博主原创文章,转载请附上博文链接!

相关标签: android