是否有一种简单的方法可以在复选框控件中的复选框和相关文本之间添加填充?
我不能只添加前导空格,因为我的标签是多行。
就像,文本太接近复选框了:
是否有一种简单的方法可以在复选框控件中的复选框和相关文本之间添加填充?
我不能只添加前导空格,因为我的标签是多行。
就像,文本太接近复选框了:
当前回答
而不是调整复选框的文本,我已经做了以下事情,它为我所有的设备工作。 1)在XML中,依次添加复选框和textview;保持一定距离。 2)设置复选框文本大小为0sp。 3)在复选框旁边的textview中添加相对文本。
其他回答
我有同样的问题与Galaxy S3 mini (android 4.1.2),我只是让我的自定义复选框扩展AppCompatCheckBox而不是checkbox。现在它工作得很完美。
为什么不扩展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" />
我想这个能帮到你
<复选框 android: layout_width = " wrap_content " android: layout_height = " wrap_content " android: drawablePadding = " -30 dp” android: paddingLeft = " 30 dp " android: drawableLeft = " @drawable /检查” />
我所做的,是有一个TextView和一个复选框内(相对)布局。TextView显示我想让用户看到的文本,而复选框没有任何文本。这样,我可以在任何我想要的地方设置复选框的位置/填充。
因为你可能会为你的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值。