有没有一种简单的方法可以在CheckBox控件中复选框和相关文本之间添加填充?
我不能只是添加前导空格,因为我的标签是多行的。
目前,文本与复选框之间的距离太近了:
为了在勾选标记和文本之间留出空格,请使用:
android:paddingLeft="10dp"
由于复选框周围包含大约5dp的填充,因此它变成了10dp以上。如果您想删除填充,请参见如何在Android CheckBox周围删除填充:
android:paddingLeft="-5dp"
android:layout_marginStart="-5dp"
android:layout_marginLeft="-5dp"
// or android:translationX="-5dp" instead of layout_marginLeft
paddingRight
的效果好使吗?我在这里(https://android--code.blogspot.com/2015/08/android-checkbox-padding.html)看到它是可以运行的。 - CoolMindCheckBox
来实现。2)在CheckBox
上添加具有左边距的FrameLayout
。 - CoolMindpackage 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中,您需要创建扩展的CheckBox,而不是创建普通的CheckBox。
<com.whatever.CheckBoxWithPaddingFix
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello there" />
在 Android 9 API 28 上,将 minHeight 和 minWidth 设置为 0dp
对我来说是最干净和最直接的解决方案:
<CheckBox
android:id="@+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minHeight="0dp"
android:minWidth="0dp" />
@Override
public int getCompoundPaddingLeft() {
// Workarround for version codes < Jelly bean 4.2
// The system does not apply the same padding. Explantion:
// https://dev59.com/mm855IYBdhLWcg3w_5qm#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:
// https://dev59.com/mm855IYBdhLWcg3w_5qm#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;
}
}
在 XML 文件中,您只需要有一个参数。
android:paddingLeft="20dp"
paddingStart
;) - Artem Mostyaev在Jelly Bean中,这种行为似乎已经改变了。 paddingLeft技巧会添加额外的填充,使文本看起来过于靠右。有其他人注意到这一点吗?
如果您有自定义图像选择器用于复选框或单选按钮,则必须设置相同的按钮和背景属性,例如:
<CheckBox
android:id="@+id/filter_checkbox_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="@drawable/selector_checkbox_filter"
android:background="@drawable/selector_checkbox_filter" />
<CheckBox
android:paddingRight="12dip" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawablePadding="-30dp"
android:paddingLeft="30dp"
android:drawableLeft="@drawable/check"
/>
android:paddingLeft
在复选框前添加空格,可以尝试使用以下方法。<androidx.appcompat.widget.AppCompatCheckBox
android:id="@+id/cbReason5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/white"
android:button="@null"
android:drawableStart="@drawable/custom_bg_checkbox"
android:drawablePadding="8dp"
android:paddingStart="16dp"
android:paddingTop="12dp"
android:paddingEnd="16dp"
android:paddingBottom="12dp"
android:text="Whatever"
android:textColor="@color/textNormal"
app:buttonCompat="@null" />
android:drawablePadding
应该能帮到您。
drawableLeft
的TextView
,并使用drawablePadding
为文本留出空间。在代码中只需切换选定和未选定状态即可。 - Muhammad Babar