Android - CheckBox和文本之间的间距

289

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

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

目前,文本与复选框之间的距离太近了: alt text


1
一个简单的解决方案是使用带有drawableLeftTextView,并使用drawablePadding为文本留出空间。在代码中只需切换选定和未选定状态即可。 - Muhammad Babar
我刚刚注意到一个名为android:drawablePadding的新xml属性,它被用来设置可绘制对象和文本之间的间距。虽然我还没有尝试过,但我认为这是谷歌针对这个问题的“事后补救”。 - Peri Hartman
https://dev59.com/mm855IYBdhLWcg3w_5qm#35572204 - Mohamed Ibrahim
只使用'android:padding="10dp"'对我有效。 - undefined
32个回答

10

为了在勾选标记和文本之间留出空格,请使用:

android:paddingLeft="10dp"

由于复选框周围包含大约5dp的填充,因此它变成了10dp以上。如果您想删除填充,请参见如何在Android CheckBox周围删除填充

android:paddingLeft="-5dp"
android:layout_marginStart="-5dp"
android:layout_marginLeft="-5dp"
// or android:translationX="-5dp" instead of layout_marginLeft

如何在文本末尾添加填充?使用margin_end会使背景未被填满。 - Hai Hack
@HaiHack,paddingRight 的效果好使吗?我在这里(https://android--code.blogspot.com/2015/08/android-checkbox-padding.html)看到它是可以运行的。 - CoolMind
抱歉,我的意思是在复选框前添加空格 @CoolMind - Hai Hack
@HaiHack,我看到两个选项。1)通过编程方式扩展CheckBox来实现。2)在CheckBox上添加具有左边距的FrameLayout - CoolMind
1
请查看我的答案,获取更好的解决方案@CoolMind https://dev59.com/mm855IYBdhLWcg3w_5qm#68556814 - Hai Hack
@HaiHack,谢谢!我记得这个属性,但是以为它没什么用。 :) - CoolMind

9
为什么不直接扩展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中,您需要创建扩展的CheckBox,而不是创建普通的CheckBox。

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

2
你应该加一个“if”的判断,来避免在操作系统大于4.2的时候执行这个操作,因为在Jelly Bean中已经“修复”了这个问题。 - Martin Marconcini
1
实际上大于等于 4.2 (17)。 - Aleks N.

7

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

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

6
我刚刚得出结论:
如果您有自定义的可绘制对象,请重写复选框并添加此方法:
@Override
public int getCompoundPaddingLeft() {

    // Workarround for version codes < Jelly bean 4.2
    // The system does not apply the same padding. Explantion:
    // https://dev59.com/mm855IYBdhLWcg3w_5qm#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:
    // https://dev59.com/mm855IYBdhLWcg3w_5qm#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;
    }

}

谢谢您的回答 :)

5

在 XML 文件中,您只需要有一个参数。

android:paddingLeft="20dp"

我相信你的意思是 paddingStart ;) - Artem Mostyaev
@ArtemMostyaev 是的 - tej shah

4

在Jelly Bean中,这种行为似乎已经改变了。 paddingLeft技巧会添加额外的填充,使文本看起来过于靠右。有其他人注意到这一点吗?


谢谢更新!我已在我的回答中添加了一条注释。 - DougW

2
如果您有自定义图像选择器用于复选框或单选按钮,则必须设置相同的按钮和背景属性,例如:

如果您有自定义图像选择器用于复选框或单选按钮,则必须设置相同的按钮和背景属性,例如:

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

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

不,您不必这样做。如果您拥有与复选框/单选按钮图标不同的自定义背景,则此方法是不可接受的。 - andr

2
<CheckBox
        android:paddingRight="12dip" />

你最好将文本放在一个单独的TextView中。 - AndrewKS
@AndrewKS - 这不是一个好的可用性想法。我希望用户能够触摸文本并选择/取消选择复选框。这也会破坏许多辅助功能,就像在网页中这样做一样。 - DougW
不利于可用性的想法。不,这并不是。将复选框和文本视图放在线性布局中,并使该线性布局可触摸。 - Falmarri
1
-1 这个回答没有解决问题,问题是关于图像和文本之间的间隙。 - David Laing
这个答案是在假设复选框和文本位于不同视图的情况下给出的。 - AndrewKS
显示剩余2条评论

2
我认为这可以帮助你。

<CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:drawablePadding="-30dp"
        android:paddingLeft="30dp"
        android:drawableLeft="@drawable/check"
        />


1
@CoolMind提供了一种不错的方法,但是不能通过android:paddingLeft在复选框前添加空格,可以尝试使用以下方法。
<androidx.appcompat.widget.AppCompatCheckBox
                android:id="@+id/cbReason5"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@android:color/white"
                android:button="@null"
                android:drawableStart="@drawable/custom_bg_checkbox"
                android:drawablePadding="8dp"
                android:paddingStart="16dp"
                android:paddingTop="12dp"
                android:paddingEnd="16dp"
                android:paddingBottom="12dp"
                android:text="Whatever"
                android:textColor="@color/textNormal"
                app:buttonCompat="@null" />

android:drawablePadding应该能帮到您。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接