先定定义一个selector
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/group_selected" android:state_checked="true"/>
<item android:drawable="@drawable/group_unselected" android:state_checked="false"/>
<item android:drawable="@drawable/group_unselected"/>
</selector>
在定义一个style
<style name="CustomCheckboxTheme" parent="@android:style/Widget.CompoundButton.CheckBox">
<item name="android:button">@drawable/selector_group</item>
</style>
引用即可
<CheckBox
android:focusable="false"
android:focusableInTouchMode="false"
android:clickable="false"
android:layout_gravity="center_horizontal"
android:textSize="20sp"
android:text="im chinese"
android:id="@+id/cb_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@style/CustomCheckboxTheme" />
自定义样式的复选框样式基本完成,在使用时发现复选框比较大,看起来比较丑,可通过scaleX 与scaleY 来完成,如下。
<CheckBox
android:focusable="false"
android:focusableInTouchMode="false"
android:clickable="false"
android:layout_gravity="center_horizontal"
android:textSize="30sp"
android:text="test"
android:scaleX="0.8"
android:scaleY="0.8"
android:id="@+id/cb_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@style/CustomCheckboxTheme" />
效果图 如下: