Add margin between a RadioButton and its label in Android?(在 Android 中的 RadioButton 及其标签之间添加边距?)
问题描述
是否可以在仍然使用 Android 的内置组件的同时在 RadioButton 和标签之间添加一点空间?默认情况下,文本看起来有些皱缩.
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"/>
我已经尝试了几件事:
指定边距和内边距似乎会在整个元素(按钮和文本一起)周围添加空间.这是有道理的,但不能满足我的需要.
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.
通过 XML 创建一个自定义可绘制对象,为选中和未选中状态指定图像,然后在每个图像的右侧添加一些额外的像素.这应该可行,但现在您正在超出默认 UI.(不是世界末日,但也不理想)
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)
在每个标签的开头添加额外的空格.Android 似乎修剪了前导空格字符,如My String",但指定 unicode U+00A0,如u00A0My String"就可以了.这行得通,但似乎有点脏.
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.
有更好的解决方案吗?
推荐答案
对于现在阅读本文的任何人来说,接受的答案将导致新 API 上的一些布局问题导致过多的填充.
For anyone reading this now, the accepted answer will lead to some layout problems on newer APIs causing too much padding.
在 API <= 16 上,您可以在单选按钮上设置 paddingLeft
以设置相对于单选按钮的视图边界的填充.此外,补丁 9 的背景也会改变相对于视图的视图边界.
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.
在 API >= 17 上,paddingLeft
(或 paddingStart
)与可绘制的单选按钮相关.同样适用于补丁九.为了更好地说明填充差异,请参阅随附的屏幕截图.
On API >= 17 the paddingLeft
(or paddingStart
) 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.
如果您深入研究代码,您会在 API 17 中找到一个名为 getHorizontalOffsetForDrawables.在计算单选按钮的左填充时调用此方法(因此图片中显示了额外的空间).
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 如果您的 minSdkVersion
>= 17,请使用 paddingLeft
.如果您支持 API <= 16,您应该为 min SDK 设置单选按钮样式您正在支持 API 17+ 的另一种风格.
TL;DR Just use paddingLeft
if your minSdkVersion
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+.
这篇关于在 Android 中的 RadioButton 及其标签之间添加边距?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 Android 中的 RadioButton 及其标签之间添加边距?
基础教程推荐
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01