Android的 - 复选框和文本之间的间距 [英] Android - Spacing between CheckBox and text

查看:121
本文介绍了Android的 - 复选框和文本之间的间距的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种简单的方法在CheckBox控件的复选框之间添加填充物,以及相关的文字?

Is there an easy way to add padding between the checkbox in a CheckBox control, and the associated text?

我不能只是添加前导空格,因为我的标签是多行。

I cannot just add leading spaces, because my label is multi-line.

由于-是,该文本是太接近的复选框:

As-is, the text is way too close to the checkbox:

推荐答案

我不想回答我的问题,但在这种情况下,我想我需要。检查出来后,@Falmarri是在正确的轨道上,他的答案。问题是,Android的CheckBox控件已经使用了android:以下属性来属性来获取文本的地方是

I hate to answer my own question, but in this case I think I need to. After checking it out, @Falmarri was on the right track with his answer. The problem is that Android's CheckBox control already uses the android:paddingLeft property to get the text where it is.

红线显示了整个CheckBox的以下属性来偏移值

The red line shows the paddingLeft offset value of the entire CheckBox

如果我只是覆盖填充在我的XML的布局,它搅乱了布局。下面是设置以下属性来=0的作用:

If I just override that padding in my XML layout, it messes up the layout. Here's what setting paddingLeft="0" does:

原来你不能在XML解决这个问题。你必须做到在code。这里是我的代码段与10dp硬codeD填充增加。

Turns out you can't fix this in XML. You have do it in code. Here's my snippet with a hardcoded padding increase of 10dp.

final float scale = this.getResources().getDisplayMetrics().density;
checkBox.setPadding(checkBox.getPaddingLeft() + (int)(10.0f * scale + 0.5f),
        checkBox.getPaddingTop(),
        checkBox.getPaddingRight(),
        checkBox.getPaddingBottom());

这为您提供了以下内容,其中绿线就是增加了填充。这比硬编码的值,因为不同的设备可能使用不同的可绘制的复选框更安全。

This gives you the following, where the green line is the increase in padding. This is safer than hardcoding a value, since different devices could use different drawables for the checkbox.

更新 - 正如人们在回答以下问题,最近提到,此行为已在果冻豆(4.2)明显改变。您的应用程序将需要检查其运行哪个版本,并使用适当的方法。

UPDATE - As people have recently mentioned in answers below, this behavior has apparently changed in Jelly Bean (4.2). Your app will need to check which version its running on, and use the appropriate method.

这篇关于Android的 - 复选框和文本之间的间距的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆