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

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

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


当前回答

因为你可能会为你的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值。

其他回答

在XML文件中只需要有一个参数

android:paddingLeft="20dp"
<CheckBox
        android:paddingRight="12dip" />

在我的情况下,我解决了这个问题使用以下复选框属性在XML:

*

android: paddingLeft = " @dimen / activity_horizontal_margin”

*

你所要做的就是在你的android xml布局的复选框中添加android:singleLine="true":

<CheckBox 
   android:id="@+id/your_check_box"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:singleLine="true"
   android:background="@android:color/transparent"
   android:text="@string/your_string"/>

并且不会以编程方式添加任何特殊内容。

如果你有自定义图像选择框或单选按钮,你必须设置相同的按钮和背景属性,如下所示:

            <CheckBox
                android:id="@+id/filter_checkbox_text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:button="@drawable/selector_checkbox_filter"
                android:background="@drawable/selector_checkbox_filter" />

您可以用背景属性控制复选框或单选按钮填充的大小。