是否有一种简单的方法可以在复选框控件中的复选框和相关文本之间添加填充?
我不能只添加前导空格,因为我的标签是多行。
就像,文本太接近复选框了:
是否有一种简单的方法可以在复选框控件中的复选框和相关文本之间添加填充?
我不能只添加前导空格,因为我的标签是多行。
就像,文本太接近复选框了:
当前回答
如果你有自定义图像选择框或单选按钮,你必须设置相同的按钮和背景属性,如下所示:
<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" />
您可以用背景属性控制复选框或单选按钮填充的大小。
其他回答
如果您正在创建自定义按钮,例如,请参阅更改复选框的外观教程
然后简单地增加btn_check_label_background.9.png的宽度,在图像中心增加一到两列透明像素;让9个补丁标记保持原样。
如果你有自定义图像选择框或单选按钮,你必须设置相同的按钮和背景属性,如下所示:
<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" />
您可以用背景属性控制复选框或单选按钮填充的大小。
设置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;
}
}
谢谢你的回答:)
在我的情况下,我解决了这个问题使用以下复选框属性在XML:
*
android: paddingLeft = " @dimen / activity_horizontal_margin”
*