如何自定义CheckBox多选框的样式

安卓自带的CheckBox有时候不能满足项目需求,这时候就需要自己定制一套样式出来。具体实现方法如下:

第一:首先需要找几张自己想要定义成的图片:
这里写图片描述 这里写图片描述
这里写图片描述 这里写图片描述

把上面的图片放到drawable-xhdpi或者mipmap目录下。

第二:在res目录下面创建drawable文件夹,drawable下面创建xml文件checkbox_selector.xml代码如下:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_checked="false"
        android:state_pressed="true"
        android:drawable="@drawable/grid_check_off_press"></item>
    <item android:state_checked="false"
        android:drawable="@drawable/grid_check_off"></item>
    <item android:state_checked="true"
        android:state_pressed="true"
        android:drawable="@drawable/grid_check_on_press" />
    <item android:state_checked="true"
        android:drawable="@drawable/grid_check_on"></item>

</selector>

到这里工作已经完成1/2了。

第三:在stytle文件中新建样式:

<style name="mycheckbox" parent="@android:style/Widget.CompoundButton.CheckBox">
        <item name="android:button">@drawable/checkbox_selectors</item>
        <item name="android:paddingLeft">10.0dip</item>
        <item name="android:height">35.0dip</item>
</style>

第四:在布局中使用就好啦,引入stytle样式。

<CheckBox
        android:id="@+id/checkBox1"
        style="@style/mycheckbox"
        android:layout_width="wrap_content"
        android:layout_height="35dp"
        android:layout_alignLeft="@+id/tv"
        android:layout_below="@+id/tv"
        android:layout_marginTop="84dp"
        android:text="自定义样式" />

点此下载源码

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值