是否有一种简单的方法可以在复选框控件中的复选框和相关文本之间添加填充?
我不能只添加前导空格,因为我的标签是多行。
就像,文本太接近复选框了:
是否有一种简单的方法可以在复选框控件中的复选框和相关文本之间添加填充?
我不能只添加前导空格,因为我的标签是多行。
就像,文本太接近复选框了:
当前回答
也许已经太晚了,但是我已经创建了实用工具方法来管理这个问题。
只需将这些方法添加到您的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);
其他回答
API 17及以上,你可以使用:
android:paddingStart=“24dp”
API 16及以下,你可以使用:
android:paddingLeft=“24dp”
设置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" />
如果您正在创建自定义按钮,例如,请参阅更改复选框的外观教程
然后简单地增加btn_check_label_background.9.png的宽度,在图像中心增加一到两列透明像素;让9个补丁标记保持原样。
你所要做的就是在你的android xml布局的复选框中添加android:singleLine="true":
<CheckBox
android:id="@+id/your_check_box"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:background="@android:color/transparent"
android:text="@string/your_string"/>
并且不会以编程方式添加任何特殊内容。
使用属性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"
/>