是否有一种简单的方法可以在复选框控件中的复选框和相关文本之间添加填充?
我不能只添加前导空格,因为我的标签是多行。
就像,文本太接近复选框了:
是否有一种简单的方法可以在复选框控件中的复选框和相关文本之间添加填充?
我不能只添加前导空格,因为我的标签是多行。
就像,文本太接近复选框了:
当前回答
如果有人需要填充周围的可绘制尝试这
<com.google.android.material.checkbox.MaterialCheckBox
android:id="@+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="@null"
android:drawableStart="@drawable/button_selector"
android:padding="@dimen/items_padding" />
其他回答
我通过改变图像来解决这个问题。 刚刚在png文件中添加了额外的透明背景。 这个解决方案在所有api上都非常有效。
如果你想要一个没有代码的干净的设计,使用:
<CheckBox
android:id="@+id/checkBox1"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:drawableLeft="@android:color/transparent"
android:drawablePadding="10dp"
android:text="CheckBox"/>
诀窍是为android: drawablleft设置颜色为透明,并为android:drawablePadding赋值。此外,透明度允许你在任何背景颜色上使用这种技术,而不会产生类似颜色不匹配的副作用。
是的,您可以通过添加填充来添加填充。
android:衬垫= 5dp
而不是调整复选框的文本,我已经做了以下事情,它为我所有的设备工作。 1)在XML中,依次添加复选框和textview;保持一定距离。 2)设置复选框文本大小为0sp。 3)在复选框旁边的textview中添加相对文本。
您需要获取正在使用的图像的大小,以便将填充添加到此大小。在Android内部,它们获取你在src上指定的drawable,然后使用它的大小。因为它是一个私有变量,没有getter,你不能访问它。同样,你也不能得到com.android.internal.R.styleable.CompoundButton并从那里得到drawable。
所以你需要创建你自己的可样式(即custom_src),或者你可以直接在你的RadioButton的实现中添加它:
public class CustomRadioButton extends RadioButton {
private Drawable mButtonDrawable = null;
public CustomRadioButton(Context context) {
this(context, null);
}
public CustomRadioButton(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public CustomRadioButton(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
mButtonDrawable = context.getResources().getDrawable(R.drawable.rbtn_green);
setButtonDrawable(mButtonDrawable);
}
@Override
public int getCompoundPaddingLeft() {
if (Util.getAPILevel() <= Build.VERSION_CODES.JELLY_BEAN_MR1) {
if (drawable != null) {
paddingLeft += drawable.getIntrinsicWidth();
}
}
return paddingLeft;
}
}