一行代码搞定checkbox文字和复选框的图片间距问题 rediobutton同理

本文介绍了解决Android中CheckBox在不同版本显示不一致的问题。通过使用drawablePadding属性调整图标与文字间距,确保应用在所有Android版本上表现一致。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

废话不说先上问题中的代码和效果图片



<CheckBox
    android:layout_marginTop="10dp"
    android:layout_marginLeft="15dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:button="@null"
    android:drawableLeft="@drawable/selector_checkbox_button"
    android:text="本人已同意以上条款"
    android:textSize="16sp"
    android:textColor="@color/colorText02"
    />



一般项目中都会用到checkbox和rediobutton 这样的按钮  按钮旁边经常也会有对应的文字,由于原生的单选框和复选框图片实在是太丑了,所以一般都会自定义图标:

android:drawableLeft="@drawable/selector_checkbox_button"
但是文字和图标距离太近,于是加了: android:paddingLeft="5dp",结果发现4.2以上的手机上效果正常,4.2一下的手机显示的文字和复选框重叠了

查了一些资料,发现原来4.2一下的版本中没有加上图标的宽度,4.2以上中计算时加上了图标的宽度,这样以来难道还要我们在代码中判断手机版本吗?答案肯定是NO  其实还有这些button还有一个属性 :drawablePadding 这个是button上下左右图片的内边距 可以设置文字和图片的间距

android:drawablePadding="8dp"


好了,再来看下  加上drawablepaddingd效果吧

代码部分:

<CheckBox
    android:layout_marginTop="10dp"
    android:layout_marginLeft="15dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:button="@null"
    android:drawableLeft="@drawable/selector_checkbox_button"
    android:text="@string/text001"
    android:textSize="16sp"
    android:textColor="@color/colorText02"
    android:drawablePadding="8dp"
    />

效果图:


最后说下  rediobutton  也可以这样的实现  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值