【背景需求】
?Android中CheckBox的使用頻率是相當(dāng)高的,但是很多時(shí)候系統(tǒng)自帶的樣式和效果并不能滿足實(shí)際項(xiàng)目需求夭织,所以知道怎么去自定義樣式和擴(kuò)大CheckBox點(diǎn)擊區(qū)域還是相當(dāng)重要的吭露。
【效果】
樣式.png
【核心】
【理解】
既然CheckBox繼承了CompoundButton,那么我們可以把CheckBox當(dāng)成一個(gè)Button來進(jìn)行處理尊惰,下面便是通過設(shè)置android:drawableLeft解決這一問題讲竿。
<!--自定義樣式+擴(kuò)大點(diǎn)擊區(qū)域CheckBox-->
<CheckBox
android:id="@+id/checkbox3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="@null"
android:drawableLeft="@drawable/bg_checkbox_one“
android:drawableStart="@drawable/bg_checkbox_one"
android:padding="10dp"/>
【bg_checkbox_one 樣式】
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@mipmap/icon_check_selected"
android:state_checked="true"/>
<item android:drawable="@mipmap/icon_check_selected"
android:state_selected="true"/>
<item android:drawable="@mipmap/icon_check_selected"
android:state_pressed="true"/>
<item android:drawable="@mipmap/icon_check_default"
android:state_checked="false"/>
</selector>