是否有一种简单的方法可以在复选框控件中的复选框和相关文本之间添加填充?
我不能只添加前导空格,因为我的标签是多行。
就像,文本太接近复选框了:
是否有一种简单的方法可以在复选框控件中的复选框和相关文本之间添加填充?
我不能只添加前导空格,因为我的标签是多行。
就像,文本太接近复选框了:
当前回答
为什么不扩展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" />
其他回答
也许已经太晚了,但是我已经创建了实用工具方法来管理这个问题。
只需将这些方法添加到您的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);
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()引入的;
API 17及以上,你可以使用:
android:paddingStart=“24dp”
API 16及以下,你可以使用:
android:paddingLeft=“24dp”
是的,您可以通过添加填充来添加填充。
android:衬垫= 5dp
<CheckBox android:drawablePadding="16dip" -可绘制对象和文本之间的填充。