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

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

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


当前回答

API 17及以上,你可以使用:

android:paddingStart=“24dp”

API 16及以下,你可以使用:

android:paddingLeft=“24dp”

其他回答

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

为什么不扩展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" />

而不是调整复选框的文本,我已经做了以下事情,它为我所有的设备工作。 1)在XML中,依次添加复选框和textview;保持一定距离。 2)设置复选框文本大小为0sp。 3)在复选框旁边的textview中添加相对文本。

鉴于@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给你最好的选择。

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

android:衬垫= 5dp