是否有一种简单的方法可以在复选框控件中的复选框和相关文本之间添加填充?

我不能只添加前导空格,因为我的标签是多行。

就像,文本太接近复选框了:


当前回答

简单的解决方案,在CheckBox属性中添加这一行,用你想要的间距值替换10dp

android:paddingLeft="10dp"

其他回答

我不知道,但我试过了

<CheckBox android:paddingLeft="8mm"并且只将文本向右移动,而不是整个控件。

它很适合我。

因为你可能会为你的android:button属性使用一个drawable选择器,你需要添加android:constantSize="true"和/或指定一个默认的drawable,像这样:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" android:constantSize="true">
  <item android:drawable="@drawable/check_on" android:state_checked="true"/>
  <item android:drawable="@drawable/check_off"/>
</selector>

之后,你需要在你的复选框xml中指定android:paddingLeft属性。

缺点:

在布局编辑器中,你会将文本放在带有api 16及以下的复选框下,在这种情况下,你可以通过创建自定义复选框类来修复它,就像建议的api级别16一样。

理由是:

这是一个bug,因为StateListDrawable#getIntrinsicWidth()调用在CompoundButton内部使用,但如果没有当前状态,也没有使用常量大小,它可能返回< 0值。

简单的解决方案,在CheckBox属性中添加这一行,用你想要的间距值替换10dp

android:paddingLeft="10dp"

对于复选符号和文本之间的空格,使用:

android:paddingLeft="10dp"

但它会超过10dp,因为选中标记周围包含填充(大约5dp)。如果你想删除填充,请参见如何删除Android复选框周围的填充:

android:paddingLeft="-5dp"
android:layout_marginStart="-5dp"
android:layout_marginLeft="-5dp"
// or android:translationX="-5dp" instead of layout_marginLeft

API 17及以上,你可以使用:

android:paddingStart=“24dp”

API 16及以下,你可以使用:

android:paddingLeft=“24dp”