android 自定义 checkbox
16lz
2021-12-04
<?xml version="1.0" encoding="utf-8">
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:state_pressed="true"
android:drawable="@drawable/focused"/>
<item android:state_checked="false" android:state_pressed="true"
android:drawable="@drawable/normal"/>
<item android:state_checked="false" android:drawable="@drawable/normal"/>
<item android:state_checked="true" android:drawable="@drawable/focused"/>
</selector>
state_checked选中状态state_pressed按下状态
即分别设置checkbox选中和没选中时按下和没按下时显示的图片。
应用到checkbox与button不同,并不是设置background属性,而是设置style属性,
所以我们要写一个style。
strings.xml写一个style.
<style name="MyCheckBox" parent="@android:style/Widget.CompoundButton.CheckBox">
<item name="android:button">@drawable/checkbox</item>
应用到checkbox:
<CheckBox android:layout_width="wrap_content"
android:layout_height="wrap_content" style="@style/MyCheckBox"/>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:state_pressed="true"
android:drawable="@drawable/focused"/>
<item android:state_checked="false" android:state_pressed="true"
android:drawable="@drawable/normal"/>
<item android:state_checked="false" android:drawable="@drawable/normal"/>
<item android:state_checked="true" android:drawable="@drawable/focused"/>
</selector>
state_checked选中状态state_pressed按下状态
即分别设置checkbox选中和没选中时按下和没按下时显示的图片。
应用到checkbox与button不同,并不是设置background属性,而是设置style属性,
所以我们要写一个style。
strings.xml写一个style.
<style name="MyCheckBox" parent="@android:style/Widget.CompoundButton.CheckBox">
<item name="android:button">@drawable/checkbox</item>
应用到checkbox:
<CheckBox android:layout_width="wrap_content"
android:layout_height="wrap_content" style="@style/MyCheckBox"/>
更多相关文章
- Android(安卓)ProgressBar的使用
- Android(安卓)RelativeLayout用到的一些重要的属性
- ProgressBar的样式及用法
- wifi和GPRS下获取android的IP信息
- 安卓布局
- Android(安卓)Activity onConfigurationChanged()方法 监听状态
- [长姿势了]android:padding和android:margin的区别
- textview中有很多行,我只让它显示三行,最后显示...,我设置了androi
- android:layout_gravity和android:gravity属性的区别