checkbox自定义样式

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
版权声明:本文为博主原创文章,转载请附上博文链接!

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值