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

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

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


当前回答

我通过改变图像来解决这个问题。 刚刚在png文件中添加了额外的透明背景。 这个解决方案在所有api上都非常有效。

其他回答

是的,您可以通过添加填充来添加填充。

android:衬垫= 5dp

因为你可能会为你的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
        android:paddingRight="12dip" />

你所要做的就是在你的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"/>

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

鉴于@DougW的响应,我所做的管理版本更简单,我添加到我的复选框视图:

android:paddingLeft="@dimen/padding_checkbox"

在两个值文件夹中找到dimen:

<resources>

    <dimen name="padding_checkbox">0dp</dimen>

</resources>

values-v17 (4.2 JellyBean)

<resources>

    <dimen name="padding_checkbox">10dp</dimen>

</resources>

我有定制check,用dps给你最好的选择。