在 Android 中添加 RadioButton 及其标签之间的边距?
是否可以在 RadioButton 和标签之间添加一点空间,同时仍然使用 Android 的内置组件?默认情况下,文本看起来有点皱巴巴的。
<RadioButton android:id="@+id/rb1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="My Text"/>
我尝试了一些方法:
指定边距和填充似乎会在整个元素(按钮和文本一起)周围添加空间。这是有道理的,但没有达到我的需要。
通过 XML 创建自定义可绘制对象,指定选中和未选中状态的图像,然后在每个图像的右侧添加一些额外的像素。这应该可行,但现在您正在走出默认 UI。 (不是世界末日,但并不理想)
在每个标签的开头添加额外的空格。 Android 似乎会修剪前导空格字符,如“My String”,但指定 unicode U+00A0,如“\u00A0My String”即可解决问题。这可行,但看起来有点脏。
还有更好的解决方案吗?
Is it possible to add a little bit of space between a RadioButton and the label while still using Android's built-in components? By default the text looks a little scrunched.
<RadioButton android:id="@+id/rb1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="My Text"/>
I've tried a couple of things:
Specifying margin and padding seem to add space around the entire element (button and text, together). That makes sense, but doesn't do what I need.
Creating a custom drawable via XML specifying images for the checked and unchecked states, then adding a few extra pixels to the right side of each image. This should work, but now you are stepping outside the default UI. (Not the end of the world, but not ideal)
Add extra whitespace to the beginning of each label. Android seems to trim a leading space character, as in " My String", but specifying unicode U+00A0, as in "\u00A0My String" does the trick. This works, but it seems kinda dirty.
Any better solutions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(19)
对于现在阅读本文的任何人来说,接受的答案将导致新 API 上出现一些布局问题,导致填充过多。
在 API <= 16 上,您可以在单选按钮上设置
paddingLeft
以设置相对于单选按钮的视图边界的填充。此外,补丁九背景还会更改相对于视图的视图边界。在 API >= 17 上,
paddingLeft
(或paddingStart
)与单选按钮可绘制相关。这同样适用于补丁九。为了更好地说明填充差异,请参阅随附的屏幕截图。如果你深入研究代码,你会发现 API 17 中有一个名为 getHorizontalOffsetForDrawables。在计算单选按钮的左填充(因此图中所示的附加空间)时调用此方法。
TL;DR 如果您的
minSdkVersion
>= 17,则只需使用paddingLeft
。如果您支持 API <= 16,您应该为您的最小 SDK 设置单选按钮样式支持 API 17+ 的另一种风格。For anyone reading this now, the accepted answer will lead to some layout problems on newer APIs causing too much padding.
On API <= 16 you can set
paddingLeft
on the radio button to set the padding relative to the radio button's view bounds. Additionally, a patch nine background also changes the view bounds relative to the view.On API >= 17 the
paddingLeft
(orpaddingStart
) is in relation to the radio button drawable. Same applies to the about a patch nine. To better illustrate padding differences see the attached screenshot.If you dig through the code you will find a new method in API 17 called getHorizontalOffsetForDrawables. This method is called when calculating the left padding for a radio button(hence the additional space illustrated in the picture).
TL;DR Just use
paddingLeft
if yourminSdkVersion
is >= 17. If you support API <= 16, you should have radio button style for the min SDK you are supporting and another style for API 17+.不确定这是否能解决您的问题,但您是否尝试过单选按钮的“Padding left”属性,其值为 50dip 或更多
Not sure if this will fix your problem, but have you tried Radio Button's "Padding left" property with a value of 50dip or more
我尝试了几种方法,并最终在模拟器和设备上都能正常工作:
i tried several ways and finished with this one working correctly on both emulator and devices:
通过 paddingLeft 在单选按钮及其标签之间添加边距:
只需设置您的自定义内边距 。
RadioButton 的 xml 属性。
完成
Add margin between a radiobutton its label by paddingLeft:
Just set your custom padding.
RadioButton's xml property.
Done
使用以下 XML 属性。它对我有用
For API <= 16 use
For API >= 17 use
例如:
更多信息:
drawablePadding
属性不起作用。仅当您在单选按钮中添加了可绘制对象时,它才有效。例如:Use the following XML attributes. It worked for me
For API <= 16 use
For API >= 17 use
Eg:
Further More:
drawablePadding
attribute doesn't work. It only works if you added a drawable in your radio button. For Eg:现在无法尝试此验证,但您是否尝试过查看属性 android:drawablePadding 能满足您的需要吗?
Can't try this right now to verify, but have you tried to see if the attribute android:drawablePadding does what you need?
可绘制对象和文本之间的填充。这将通过在 xml 文件中添加以下行来实现。
android:drawablePadding="@dimen/10dp"
The padding between the drawables and the text. It will be achieved by adding line below in xml file.
android:drawablePadding="@dimen/10dp"
对于 16 以上的 API,只需使用 padding start
jusst use padding start for API above 16
我试过了,“android:paddingLeft”会起作用。 paddingLeft 只会影响文本,同时保持无线电图像保持在原始位置。
I have tried, "android:paddingLeft" will works. paddingLeft will only impact the text while keep the radio image stay at the original position.
“android:paddingLeft”似乎只能在 android 4.2.2 下正常工作
我已经尝试了几乎所有版本的 android,它只适用于 4.2.2 版本。
The "android:paddingLeft" only seems to work correctly under android 4.2.2
i have tried almost all versions of android and it only works on the 4.2.2 version.
我正在使用不同的方法,我认为该方法应该适用于所有 API 版本。
我没有应用填充,而是在 RadioButtons 之间添加一个空视图:
这应该为您提供 20dp 的填充。
I'm using different approach that I think should work on all API versions.
Instead of applying padding I'm adding an empty view between to RadioButtons:
This should give you 20dp padding.
我来这里寻找答案,最简单的方法(经过一番思考)是在标签本身的开头添加间距,如下所示
I came here looking for an answer and the simplest way (after some thinking) was add spacing at the beginning of the label itself like so
对我来说有效:
for me works:
您可以在 XML 文件上使用此代码
或在 Activity 类上使用此代码
You can this code on your XML file
or use this on Activity class
在 style.xml 中创建一个样式,如下所示
将该样式放入单选按钮中
您可以更改与按钮相同的任何属性,因为 RadioButton 间接继承了按钮。
Create a style in style.xml like this
Put that style in radio button
You can change any attribute same as button as it RadioButton indirectly inherits button.
我知道这是一个老问题,但是有了这个解决方案,我终于安心了,忘记了 API 级别。
左侧垂直 RadioGroup 与右侧垂直文本视图。
I know it is an old question, but with this solution, I finally got peace of mind and forget about API level.
Left side vertical RadioGroup with right side vertical text view.
您可以尝试使用单选按钮的重力属性。
这会将文本与最右端对齐。查看图像中的第一个收音机。
You could try using the gravity attribute of radio button .
This will align the text to the right most end . Check out the first radio in the image.