1、设置CheckBox选中/不选中,按下/不按下状态的样式
<1>、在drawable中配置checkbox.xml
<?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>
<2>、在res/values/styles.xml文件中添加MyCheckBox名称的Style
<style name="MyCheckBox">
<item name="android:button">@drawable/checkbox</item>
</style>
<3>、在布局文件中设置CheckBox的style属性,如:
<CheckBox android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@style/MyCheckBox"/>
2、当CheckBox作为ListView中ListItem的子组件时,ChekBox要有下面三个配置
android:focusable="false"
因为checkbox的点击事件优先级比listview的高,所以要在checkbox中添加android:focusable="false",使得checkbox初始的时候没有获取焦点。
本文详细介绍了如何在Android中自定义CheckBox的选中、不选中、按下及不按下的样式,并在ListView中使用自定义CheckBox。通过在drawable目录下创建checkbox.xml文件来配置不同状态下的Drawable资源,然后在styles.xml文件中为CheckBox定义名为MyCheckBox的样式。最后,在布局文件中应用该样式,确保CheckBox在ListView中正常显示并避免冲突。
401

被折叠的 条评论
为什么被折叠?



