checkbox 设定文字和选框之间的距离及自定义样式

改变checkbox默认的文字与选框之间的距离两种方案,刚刚开始接触checkbox 的时候由于不熟悉控件,使用了组合控件:一个未设定text的CheckBox和一个TextView来达到目的。后来发现只需要设定CheckBox的android:paddinLeft的值即可,没有看源码,不清楚问什么CheckBox的这个属性代表的意义为什么和其他widget不一样,问题是解决了。

 

在开发过程中经常遇到的问题是,不同厂商的checkbox控件的UI不同,导致开发出的产品在UI上有些区别,尤其是一些厂商的UI做得比较丑,这时候我们可以使用自定义Checkbox来达到我们想要的UI。

只需要在drawable文件下设定一个xml即可:

  checkboxselector:

<?xml version="1.0" encoding="UTF-8"?>
<selector
  xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_window_focused="false" android:state_enabled="true" android:state_checked="true" android:drawable="@drawable/btn_check_on" />
    <item android:state_window_focused="false" android:state_enabled="true" android:state_checked="false" android:drawable="@drawable/btn_check_off" />
    <item android:state_enabled="true" android:state_checked="true" android:state_pressed="true" android:drawable="@drawable/btn_check_on_pressed" />
    <item android:state_enabled="true" android:state_checked="false" android:state_pressed="true" android:drawable="@drawable/btn_check_off_pressed" />
    <item android:state_focused="true" android:state_enabled="true" android:state_checked="true" android:drawable="@drawable/btn_check_on_selected" />
    <item android:state_focused="true" android:state_enabled="true" android:state_checked="false" android:drawable="@drawable/btn_check_off_selected" />
    <item android:state_enabled="true" android:state_checked="false" android:drawable="@drawable/btn_check_off" />
    <item android:state_enabled="true" android:state_checked="true" android:drawable="@drawable/btn_check_on" />
</selector>

 在需要使用的CheckBox控件下使用android:button属性:

    <CheckBox
        android:id="@+id/btn_check"
        android:button="@drawable/checkboxselector"
        android:layout_width="wrap_content"
        android:text="测试一下"
        android:paddingLeft="30.0dp"
        android:layout_height="wrap_content" />

 

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值