今天才发现CheckBox有个属性为button,汗颜啊。。。
尝试了使用button属性和style属性,仔细一看它俩在这种用法上根本就没啥区别,style只是多绕了一个圈儿而已。
效果图:
正文
1.先准备好drawable样式selector_person,选中没选中分别对应不同颜色的圆形:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true">
<shape android:shape="oval">
<size android:height="20dp" android:width="20dp"/>
<solid android:color="@color/colorPrimary"/>
</shape>
</item>
<item android:state_checked="false">
<shape android:shape="oval">
<size android:height="20dp" android:width="20dp"/>
<solid android:color="@color/app_gray"/>
</shape>
</item>
</selector>
2.写style样式:
<style name="CustomCheckBoxTheme" parent="@android:style/Widget.CompoundButton.CheckBox">
<item name="android:button">@drawable/selector_person</item>
</style>
3.xml布局:
<CheckBox
android:gravity="center"
android:layout_width="40dp"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:button="@drawable/selector_person" />
<CheckBox
android:gravity="center"
android:layout_width="40dp"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
style="@style/CustomCheckBoxTheme"/>
补充:checkBox有个方法为isChecked()返回值为true或false,用来获取是否选中。
列表相关代码可参照:GridView、ListView的简单使用 - An_nA的博客 - CSDN博客 http://blog.csdn.net/an_nal/article/details/76147576