是否有一种简单的方法可以在复选框控件中的复选框和相关文本之间添加填充?
我不能只添加前导空格,因为我的标签是多行。
就像,文本太接近复选框了:
是否有一种简单的方法可以在复选框控件中的复选框和相关文本之间添加填充?
我不能只添加前导空格,因为我的标签是多行。
就像,文本太接近复选框了:
当前回答
对于复选符号和文本之间的空格,使用:
android:paddingLeft="10dp"
但它会超过10dp,因为选中标记周围包含填充(大约5dp)。如果你想删除填充,请参见如何删除Android复选框周围的填充:
android:paddingLeft="-5dp"
android:layout_marginStart="-5dp"
android:layout_marginLeft="-5dp"
// or android:translationX="-5dp" instead of layout_marginLeft
其他回答
我刚才的结论是:
覆盖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;
}
}
谢谢你的回答:)
而不是调整复选框的文本,我已经做了以下事情,它为我所有的设备工作。 1)在XML中,依次添加复选框和textview;保持一定距离。 2)设置复选框文本大小为0sp。 3)在复选框旁边的textview中添加相对文本。
使用属性android: drawablleft而不是android:button。为了设置可绘制和文本之间的填充,使用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"
/>
因为你可能会为你的android:button属性使用一个drawable选择器,你需要添加android:constantSize="true"和/或指定一个默认的drawable,像这样:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" android:constantSize="true">
<item android:drawable="@drawable/check_on" android:state_checked="true"/>
<item android:drawable="@drawable/check_off"/>
</selector>
之后,你需要在你的复选框xml中指定android:paddingLeft属性。
缺点:
在布局编辑器中,你会将文本放在带有api 16及以下的复选框下,在这种情况下,你可以通过创建自定义复选框类来修复它,就像建议的api级别16一样。
理由是:
这是一个bug,因为StateListDrawable#getIntrinsicWidth()调用在CompoundButton内部使用,但如果没有当前状态,也没有使用常量大小,它可能返回< 0值。
<CheckBox android:drawablePadding="16dip" -可绘制对象和文本之间的填充。