Android - CheckBox和文本之间的间距

Dou*_*ugW 246 checkbox android padding

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

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

原样,文本太靠近复选框: 替代文字

Dou*_*ugW 327

我讨厌回答我自己的问题,但在这种情况下,我认为我需要.检查完毕后,@ Falmarri的回答是正确的.问题是Android的CheckBox控件已经使用android:paddingLeft属性来获取文本所在的位置.

红线显示整个CheckBox的paddingLeft偏移值

替代文字

如果我只是在我的XML布局中覆盖该填充,它会弄乱布局.这是paddingLeft ="0"的设置:

替代文字

事实证明,你无法在XML中解决这个问题.你已经在代码中做到了.这是我的代码片段,硬编码填充增加了10dp.

final float scale = this.getResources().getDisplayMetrics().density;
checkBox.setPadding(checkBox.getPaddingLeft() + (int)(10.0f * scale + 0.5f),
        checkBox.getPaddingTop(),
        checkBox.getPaddingRight(),
        checkBox.getPaddingBottom());
Run Code Online (Sandbox Code Playgroud)

这将为您提供以下内容,其中绿线是填充的增加.这比硬编码值更安全,因为不同的设备可以为复选框使用不同的drawable.

替代文字

更新 - 正如人们最近在下面的答案中提到的,这种行为在果冻豆(4.2)中显然已经发生了变化.您的应用需要检查其运行的版本,并使用适当的方法.

对于4.3+,它只是设置padding_left.有关详细信息,请参阅htafoya的回答.

  • 回答你自己的问题没有错 - 将来其他人可能会遇到这个问题,你只是给了一个很好的,全面的答案. (103认同)
  • @harpo - 您无法保证不同的设备制造商不会为复选框使用不同的图形,这意味着您的硬编码填充可能会破坏您在不同设备上的可视布局.因为我正在阅读当前的填充并添加它不会有问题,但你必须确保你只做一次. (5认同)
  • 设置`paddingLeft ="48sp"`有什么问题?这里工作正常,即使我改变了规模和方向.您的代码在项目列表中给了我不稳定的填充值. (4认同)
  • @Blundell - 不一定.Android的开源.设备供应商可以用完全不同的图形替换完全不同的图形.出于某种原因,他们喜欢做那样的事情. (2认同)
  • 不幸的是,你不能在自定义适配器的`getView()`中使用这个解决方案(如果你回收你的视图),因为填充将无休止地增加.为了解决这个问题,我不得不使用这样的东西:`boolean isHackNeeded = Build.VERSION.SDK_INT <17; float scale = context.getResources().getDisplayMetrics().density; if(isHackNeeded){CheckBox rb = new CheckBox(context); this.PADDING = rb.getPaddingLeft()+ Math.round(10.0f*scale); } else {this.PADDING = Math.round(10.0f*scale); }`.然后对每个我膨胀的CheckBox使用`PADDING`:`check.setPadding(PADDING,check.getPaddingTop()...` (2认同)

hta*_*oya 127

鉴于@DougW响应,我做的管理版本更简单,我添加到我的复选框视图:

android:paddingLeft="@dimen/padding_checkbox"
Run Code Online (Sandbox Code Playgroud)

在两个值文件夹中找到dimen:

<resources>

    <dimen name="padding_checkbox">0dp</dimen>

</resources>
Run Code Online (Sandbox Code Playgroud)

values-v17(4.2 JellyBean)

<resources>

    <dimen name="padding_checkbox">10dp</dimen>

</resources>
Run Code Online (Sandbox Code Playgroud)

我有自定义检查,使用dps作为您的最佳选择.

  • 到目前为止,这是最好的答案.虽然它应该是'values-v17`,因为API 17中引入了修复(根据上面的一些帖子) (2认同)

Rom*_*ets 70

使用属性android:drawableLeft而不是android:button.为了在drawable和text之间设置填充android:drawablePadding.定位可绘制用途android:paddingLeft.

<CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:button="@null"
        android:drawableLeft="@drawable/check_selector"
        android:drawablePadding="-50dp"
        android:paddingLeft="40dp"
        />
Run Code Online (Sandbox Code Playgroud)

结果

  • 设置`android:background ="@ android:color/transparent"`并且左边距将消失 (4认同)
  • 上面的代码完美地适用于我的单选按钮,并且在4.2 OS边界上保持一致.谢谢! (2认同)

kot*_*ucz 34

Android 4.2 Jelly Bean(API 17)将文本paddingLeft置于buttonDrawable(右侧边缘).它也适用于RTL模式.

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

您可以通过XML解决它 - 在旧的机器人上将paddingLeft设置为buttonDrawable.width + requiredSpace.仅在API 17上将其设置为requiredSpace.例如,使用维度资源并覆盖values-v17资源文件夹.

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

  • 这是唯一正确的答案 - 它确定了问题并提供了简单的解决方案,我在多个商业应用程序中成功使用了这些解决方案. (4认同)

Fal*_*rri 27

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

android:padding=5dp

  • 这实际上有用......有点儿.首先,只需要paddingLeft,而不是所有填充.但是,看起来paddingLeft已经设置为大约40dp的值.如果我将它设置为> 40dp它会移开,如果我将其设置为<40dp,它会移动到复选框下面.我打算将此标记为正确并打开另一个问题,因为我对此表示担忧. (4认同)

Chu*_*ham 20

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

<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"/>
Run Code Online (Sandbox Code Playgroud)

诀窍是将颜色设置为透明android:drawableLeft并为其指定值android:drawablePadding.此外,透明度允许您在任何背景颜色上使用此技术,而没有副作用 - 如颜色不匹配.


小智 16

API 17及以上版本,您可以使用:

机器人:paddingStart = "24dp"

API 16及以下版本,您可以使用:

机器人:paddingLeft = "24dp"

  • 非常好的答案。我无法想象填充如何在复选框和文本之间留出空间。通常,Padding Left 是指组件的左侧。 (2认同)

Moh*_*ria 14

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

*

机器人:paddingLeft = "@扪/ activity_horizo​​ntal_margin"

*


Nav*_*mad 13

简单的解决方案是,在CheckBox属性中添加此行,将10dp替换为所需的间距值

android:paddingLeft="10dp"
Run Code Online (Sandbox Code Playgroud)


w.d*_*hue 10

为什么不扩展Android CheckBox以获得更好的填充.这样,每次使用CheckBox时都不必在代码中修复它,您只需使用固定的CheckBox即可.

First Extend CheckBox:

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));
    }
}
Run Code Online (Sandbox Code Playgroud)

在你的xml中的第二个而不是创建一个普通的CheckBox创建你的扩展

<com.whatever.CheckBoxWithPaddingFix
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello there" />
Run Code Online (Sandbox Code Playgroud)

  • 你应该添加一个'if'以避免在操作系统> 4.2时这样做,因为在Jelly Bean中他们"修复"了这个. (2认同)

Tia*_*ago 9

我不认识男人,但我测试过

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

它很适合我.


Coo*_*ind 9

对于复选标记和文本之间的空间,请使用:

android:paddingLeft="10dp"
Run Code Online (Sandbox Code Playgroud)

但是变成了10dp以上,因为复选标记周围有padding(大约5dp)。如果要删除填充,请参阅如何删除 Android CheckBox 周围的填充

android:paddingLeft="-5dp"
android:layout_marginStart="-5dp"
android:layout_marginLeft="-5dp"
// or android:translationX="-5dp" instead of layout_marginLeft
Run Code Online (Sandbox Code Playgroud)


PaN*_*TEC 6

我刚刚结束了这个:

如果您有自定义drawable,请重写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;
    }

}
Run Code Online (Sandbox Code Playgroud)

或者如果您使用系统drawable:

@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;
    }

}
Run Code Online (Sandbox Code Playgroud)

谢谢你的回答 :)


Man*_*lTS 6

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

<CheckBox
        android:id="@+id/checkbox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:minHeight="0dp"
        android:minWidth="0dp" />
Run Code Online (Sandbox Code Playgroud)


tej*_*hah 5

只需要在 xml 文件中有一个参数

android:paddingLeft="20dp"
Run Code Online (Sandbox Code Playgroud)