Android 自定义 CheckBox
在实际项目开发中,系统默认的复选框可能满足不了我们实际的项目需求,这时候就需要我们自己来定义一个复选框来美化我们的项目。
一、首先我们要准备两张图片,用来被选中与未选中的图片。
sel_check.png sel_nor.png
准备好两张图片后,我们把它放在res下的drawable下面。
二、在drawable下面创建一个checkbox_style.xml用来控制选中与未选中图片的切换
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:drawable="@drawable/sel_check"
android:state_checked="true"/>
<item
android:drawable="@drawable/sel_nor"
android:state_checked="false"/>
<item
android:drawable="@drawable/sel_nor"/>
</selector>
三、然后在style.xml中添加
<!-- CustomCheckBox theme -->
<style name="CustomCheckBoxTheme" parent="@android:style/Widget.CompoundButton.CheckBox">
<item name="android:button">@drawable/checkbox_style</item>
</style>
四、在需要使用自定义复选框的地方添加style
<CheckBox
android:id="@+id/cb_dataDownload_downloadAsset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@style/CustomCheckBoxTheme"
android:checked="true"/>
这样就大功告成了。