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

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

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


当前回答

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()引入的;

其他回答

也许已经太晚了,但是我已经创建了实用工具方法来管理这个问题。

只需将这些方法添加到您的utils:

public static void setCheckBoxOffset(@NonNull  CheckBox checkBox, @DimenRes int offsetRes) {
    float offset = checkBox.getResources().getDimension(offsetRes);
    setCheckBoxOffsetPx(checkBox, offset);
}

public static void setCheckBoxOffsetPx(@NonNull CheckBox checkBox, float offsetInPx) {
    int leftPadding;
    if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.JELLY_BEAN) {
        leftPadding = checkBox.getPaddingLeft() + (int) (offsetInPx + 0.5f);
    } else {
        leftPadding = (int) (offsetInPx + 0.5f);
    }
    checkBox.setPadding(leftPadding,
            checkBox.getPaddingTop(),
            checkBox.getPaddingRight(),
            checkBox.getPaddingBottom());
}

像这样使用:

    ViewUtils.setCheckBoxOffset(mAgreeTerms, R.dimen.space_medium);

或者像这样:

    // Be careful with this usage, because it sets padding in pixels, not in dp!
    ViewUtils.setCheckBoxOffsetPx(mAgreeTerms, 100f);

简单的解决方案,在CheckBox属性中添加这一行,用你想要的间距值替换10dp

android:paddingLeft="10dp"

你所要做的就是在你的android xml布局的复选框中添加android:singleLine="true":

<CheckBox 
   android:id="@+id/your_check_box"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:singleLine="true"
   android:background="@android:color/transparent"
   android:text="@string/your_string"/>

并且不会以编程方式添加任何特殊内容。

我不知道,但我试过了

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

它很适合我。

我想这个能帮到你

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