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

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

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


当前回答

android4.2果冻豆(API 17)把文本填充左从buttonDrawable(整数右边缘)。它也适用于RTL模式。

在4.2之前,paddingLeft忽略了buttonDrawable -它是从CompoundButton视图的左边缘取的。

你可以通过XML来解决这个问题——设置paddingLeft到buttonDrawable。width + requiredSpace在旧的机器人。只在API 17上设置为requiredSpace。例如,使用维度资源并覆盖values-v17资源文件夹。

这个变化是通过android.widget.CompoundButton.getCompoundPaddingLeft()引入的;

其他回答

<CheckBox android:drawablePadding="16dip" -可绘制对象和文本之间的填充。

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

            <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" />

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

如果你想要一个没有代码的干净的设计,使用:

<CheckBox
   android:id="@+id/checkBox1"
   android:layout_height="wrap_content"
   android:layout_width="wrap_content"
   android:drawableLeft="@android:color/transparent"
   android:drawablePadding="10dp"
   android:text="CheckBox"/>

诀窍是为android: drawablleft设置颜色为透明,并为android:drawablePadding赋值。此外,透明度允许你在任何背景颜色上使用这种技术,而不会产生类似颜色不匹配的副作用。

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

*

android: paddingLeft = " @dimen / activity_horizontal_margin”

*

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