Dou*_*ugW 246 checkbox android padding
是否有一种简单的方法可以在CheckBox控件中的复选框和相关文本之间添加填充?
我不能只添加前导空格,因为我的标签是多行的.
原样,文本太靠近复选框:

Dou*_*ugW 327
我讨厌回答我自己的问题,但在这种情况下,我认为我需要.检查完毕后,@ Falmarri的回答是正确的.问题是Android的CheckBox控件已经使用android:paddingLeft属性来获取文本所在的位置.
红线显示整个CheckBox的paddingLeft偏移值

如果我只是在我的XML布局中覆盖该填充,它会弄乱布局.这是paddingLeft ="0"的设置:

事实证明,你无法在XML中解决这个问题.你已经在代码中做到了.这是我的代码片段,硬编码填充增加了10dp.
final float scale = this.getResources().getDisplayMetrics().density;
checkBox.setPadding(checkBox.getPaddingLeft() + (int)(10.0f * scale + 0.5f),
checkBox.getPaddingTop(),
checkBox.getPaddingRight(),
checkBox.getPaddingBottom());
Run Code Online (Sandbox Code Playgroud)
这将为您提供以下内容,其中绿线是填充的增加.这比硬编码值更安全,因为不同的设备可以为复选框使用不同的drawable.

更新 - 正如人们最近在下面的答案中提到的,这种行为在果冻豆(4.2)中显然已经发生了变化.您的应用需要检查其运行的版本,并使用适当的方法.
对于4.3+,它只是设置padding_left.有关详细信息,请参阅htafoya的回答.
hta*_*oya 127
鉴于@DougW响应,我做的管理版本更简单,我添加到我的复选框视图:
android:paddingLeft="@dimen/padding_checkbox"
Run Code Online (Sandbox Code Playgroud)
在两个值文件夹中找到dimen:
值
<resources>
<dimen name="padding_checkbox">0dp</dimen>
</resources>
Run Code Online (Sandbox Code Playgroud)
values-v17(4.2 JellyBean)
<resources>
<dimen name="padding_checkbox">10dp</dimen>
</resources>
Run Code Online (Sandbox Code Playgroud)
我有自定义检查,使用dps作为您的最佳选择.
Rom*_*ets 70
使用属性android:drawableLeft而不是android:button.为了在drawable和text之间设置填充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"
/>
Run Code Online (Sandbox Code Playgroud)

kot*_*ucz 34
Android 4.2 Jelly Bean(API 17)将文本paddingLeft置于buttonDrawable(右侧边缘).它也适用于RTL模式.
在4.2 paddingLeft忽略buttonDrawable之前 - 它是从CompoundButton视图的左边缘获取的.
您可以通过XML解决它 - 在旧的机器人上将paddingLeft设置为buttonDrawable.width + requiredSpace.仅在API 17上将其设置为requiredSpace.例如,使用维度资源并覆盖values-v17资源文件夹.
这个变化是通过android.widget.CompoundButton.getCompoundPaddingLeft()引入的;
Fal*_*rri 27
是的,您可以通过添加填充来添加填充.
android:padding=5dp
Chu*_*ham 20
如果您想要一个没有代码的干净设计,请使用:
<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"/>
Run Code Online (Sandbox Code Playgroud)
诀窍是将颜色设置为透明android:drawableLeft并为其指定值android:drawablePadding.此外,透明度允许您在任何背景颜色上使用此技术,而没有副作用 - 如颜色不匹配.
小智 16
API 17及以上版本,您可以使用:
机器人:paddingStart = "24dp"
API 16及以下版本,您可以使用:
机器人:paddingLeft = "24dp"
Moh*_*ria 14
在我的情况下,我使用XML中的以下CheckBox属性解决了这个问题:
*
机器人:paddingLeft = "@扪/ activity_horizontal_margin"
*
Nav*_*mad 13
简单的解决方案是,在CheckBox属性中添加此行,将10dp替换为所需的间距值
android:paddingLeft="10dp"
Run Code Online (Sandbox Code Playgroud)
w.d*_*hue 10
为什么不扩展Android CheckBox以获得更好的填充.这样,每次使用CheckBox时都不必在代码中修复它,您只需使用固定的CheckBox即可.
First Extend CheckBox:
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));
}
}
Run Code Online (Sandbox Code Playgroud)
在你的xml中的第二个而不是创建一个普通的CheckBox创建你的扩展
<com.whatever.CheckBoxWithPaddingFix
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello there" />
Run Code Online (Sandbox Code Playgroud)
对于复选标记和文本之间的空间,请使用:
android:paddingLeft="10dp"
Run Code Online (Sandbox Code Playgroud)
但是变成了10dp以上,因为复选标记周围有padding(大约5dp)。如果要删除填充,请参阅如何删除 Android CheckBox 周围的填充:
android:paddingLeft="-5dp"
android:layout_marginStart="-5dp"
android:layout_marginLeft="-5dp"
// or android:translationX="-5dp" instead of layout_marginLeft
Run Code Online (Sandbox Code Playgroud)
我刚刚结束了这个:
如果您有自定义drawable,请重写CheckBox并添加此方法:
@Override
public int getCompoundPaddingLeft() {
// Workarround for version codes < Jelly bean 4.2
// The system does not apply the same padding. Explantion:
// http://stackoverflow.com/questions/4037795/android-spacing-between-checkbox-and-text/4038195#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;
}
}
Run Code Online (Sandbox Code Playgroud)
或者如果您使用系统drawable:
@Override
public int getCompoundPaddingLeft() {
// Workarround for version codes < Jelly bean 4.2
// The system does not apply the same padding. Explantion:
// http://stackoverflow.com/questions/4037795/android-spacing-between-checkbox-and-text/4038195#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;
}
}
Run Code Online (Sandbox Code Playgroud)
谢谢你的回答 :)
0dp在 Android 9 API 28 上,将 minHeight 和 minWidth 设置为对我来说是最干净、最直接的解决方案:
<CheckBox
android:id="@+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minHeight="0dp"
android:minWidth="0dp" />
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
128815 次 |
| 最近记录: |