在 Android 中的 RadioButton 及其标签之间添加边距?

Add margin between a RadioButton and its label in Android?(在 Android 中的 RadioButton 及其标签之间添加边距?)

本文介绍了在 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"/>

我已经尝试了几件事:

  1. 指定边距和内边距似乎会在整个元素(按钮和文本一起)周围添加空间.这是有道理的,但不能满足我的需要.

  1. 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 中找到一个名为 getHorizo​​ntalOffsetForDrawables.在计算单选按钮的左填充时调用此方法(因此图片中显示了额外的空间).

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 及其标签之间添加边距?

基础教程推荐