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

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

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


当前回答

这种行为在果冻豆中似乎发生了改变。paddingLeft技巧增加了额外的填充,使文本看起来太靠右了。有人注意到吗?

其他回答

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

<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赋值。此外,透明度允许你在任何背景颜色上使用这种技术,而不会产生类似颜色不匹配的副作用。

我不知道,但我试过了

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

它很适合我。

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属性中添加这一行,用你想要的间距值替换10dp

android:paddingLeft="10dp"

为什么不扩展Android复选框以获得更好的填充呢?这样一来,每次使用复选框时都不必在代码中修复它,而只需使用固定的复选框即可。

第一个扩展复选框:

package com.whatever;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.CheckBox;

/**
 * This extends the Android CheckBox to add some more padding so the text is not on top of the
 * CheckBox.
 */
public class CheckBoxWithPaddingFix extends CheckBox {

    public CheckBoxWithPaddingFix(Context context) {
        super(context);
    }

    public CheckBoxWithPaddingFix(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public CheckBoxWithPaddingFix(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public int getCompoundPaddingLeft() {
        final float scale = this.getResources().getDisplayMetrics().density;
        return (super.getCompoundPaddingLeft() + (int) (10.0f * scale + 0.5f));
    }
}

其次,在xml中创建一个扩展的复选框,而不是创建一个普通的复选框

<com.whatever.CheckBoxWithPaddingFix
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello there" />