CheckBox选择框和文字设置间距

由于界面美观度的需求,需要自定义CheckBox的选择框。

传统的方法是先定义一个CheckBox的自定义style,如下:

</pre><pre name="code" class="html"> <span style="font-size:14px;"><style name="CustomCheckboxTheme" parent="@android:style/Widget.CompoundButton.CheckBox">
        <item name="android:button">@drawable/checkbox_selector</item>
 </style></span>

style中使用的checkbox_selector.xml

<span style="font-size:14px;"><?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/checkbox_unselected" android:state_selected="false"/>
    <item android:drawable="@drawable/checkbox_selected" android:state_selected="true"/>
    <item android:drawable="@drawable/checkbox_unselected"/>
</selector></span>

最后在布局文件中使用style属性加上效果,就基本实现了自定义的选择框。

<CheckBox 
        android:textSize="17.0sp"   
        android:id="@+id/custom_checkbox"  
        android:text="测试"  
        android:layout_weight="1"   
        style="@style/CustomCheckboxTheme"/>
但是,以上实现的效果有个问题:checkbox自带的文字和选择框紧紧靠在一起,往往有时候界面需求是要一定的间隔的。

解决方法如下:

 <CheckBox
       android:id="@+id/hasHouse"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_marginLeft="12dp"
       android:background="@null"
       android:button="@null"
       android:clickable="true"
       android:drawableLeft="@drawable/checkbox_selector"
       android:drawablePadding="8dp"
       android:text="测试"
       android:textColor="@color/register_input"
       android:textSize="16sp" />
大概的思路是运用checkbox的drawableLeft和drawablepadding两个属性搭配实现效果,当然,这里的button和background属性得注意隐藏。

以上方法对于radiobutton同样有效。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值