是否有一种简单的方法可以在复选框控件中的复选框和相关文本之间添加填充?
我不能只添加前导空格,因为我的标签是多行。
就像,文本太接近复选框了:
是否有一种简单的方法可以在复选框控件中的复选框和相关文本之间添加填充?
我不能只添加前导空格,因为我的标签是多行。
就像,文本太接近复选框了:
当前回答
因为你可能会为你的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值。
其他回答
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()引入的;
是的,您可以通过添加填充来添加填充。
android:衬垫= 5dp
为什么不扩展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中创建一个扩展的复选框,而不是创建一个普通的复选框
<com.whatever.CheckBoxWithPaddingFix
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello there" />
在我的情况下,我解决了这个问题使用以下复选框属性在XML:
*
android: paddingLeft = " @dimen / activity_horizontal_margin”
*
我想这个能帮到你
<复选框 android: layout_width = " wrap_content " android: layout_height = " wrap_content " android: drawablePadding = " -30 dp” android: paddingLeft = " 30 dp " android: drawableLeft = " @drawable /检查” />