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

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

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


当前回答

设置minHeight和minWidth为0dp是我在Android 9 API 28上最干净和最直接的解决方案:

<CheckBox
        android:id="@+id/checkbox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:minHeight="0dp"
        android:minWidth="0dp" />

其他回答

如果您正在创建自定义按钮,例如,请参阅更改复选框的外观教程

然后简单地增加btn_check_label_background.9.png的宽度,在图像中心增加一到两列透明像素;让9个补丁标记保持原样。

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

我想这个能帮到你

<复选框 android: layout_width = " wrap_content " android: layout_height = " wrap_content " android: drawablePadding = " -30 dp” android: paddingLeft = " 30 dp " android: drawableLeft = " @drawable /检查” />

设置minHeight和minWidth为0dp是我在Android 9 API 28上最干净和最直接的解决方案:

<CheckBox
        android:id="@+id/checkbox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:minHeight="0dp"
        android:minWidth="0dp" />

我刚才的结论是:

覆盖CheckBox并添加此方法,如果你有一个自定义绘制对象:

@Override
public int getCompoundPaddingLeft() {

    // Workarround for version codes < Jelly bean 4.2
    // The system does not apply the same padding. Explantion:
    // http://stackoverflow.com/questions/4037795/android-spacing-between-checkbox-and-text/4038195#4038195

    int compoundPaddingLeft = super.getCompoundPaddingLeft();

    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
        Drawable drawable = getResources().getDrawable( YOUR CUSTOM DRAWABLE );
        return compoundPaddingLeft + (drawable != null ? drawable.getIntrinsicWidth() : 0);
    } else {
        return compoundPaddingLeft;
    }

}

或者如果你使用系统绘图:

@Override
public int getCompoundPaddingLeft() {

    // Workarround for version codes < Jelly bean 4.2
    // The system does not apply the same padding. Explantion:
    // http://stackoverflow.com/questions/4037795/android-spacing-between-checkbox-and-text/4038195#4038195

    int compoundPaddingLeft = super.getCompoundPaddingLeft();

    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
        final float scale = this.getResources().getDisplayMetrics().density;
        return compoundPaddingLeft + (drawable != null ? (int)(10.0f * scale + 0.5f) : 0);
    } else {
        return compoundPaddingLeft;
    }

}

谢谢你的回答:)