Android 自定义 CheckBox

                                        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"/>

这样就大功告成了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值