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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2134591/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 04:41:18  来源:igfitidea点击:

Add margin between a RadioButton and its label in Android?

androidradio-button

提问by allclaws

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.

是否可以在仍然使用 Android 的内置组件的同时在 RadioButton 和标签之间添加一点空间?默认情况下,文本看起来有点皱巴巴的。

<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:

我尝试了几件事:

  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.

  2. 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)

  3. 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.

  1. 指定边距和填充似乎在整个元素(按钮和文本一起)周围增加了空间。这是有道理的,但没有做我需要的。

  2. 通过 XML 为选中和未选中状态指定图像创建自定义可绘制对象,然后在每个图像的右侧添加一些额外的像素。这应该可以工作,但现在您正在走出默认 UI。(不是世界末日,但不理想)

  3. 在每个标签的开头添加额外的空格。Android 似乎修剪了一个前导空格字符,如“我的字符串”,但指定 unicode U+00A0,如“\u00A0My String”可以解决问题。这有效,但似乎有点脏。

Any better solutions?

有什么更好的解决方案吗?

回答by Jim Baca

For anyone reading this now, the accepted answer will lead to some layout problems on newer APIs causing too much padding.

对于现在阅读本文的任何人来说,接受的答案将导致新 API 上的一些布局问题,从而导致填充过多。

On API <= 16 you can set paddingLefton 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 <= 16 上,您可以设置paddingLeft单选按钮以设置相对于单选按钮视图边界的填充。此外,补丁九背景也会改变相对于视图的视图边界。

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 上,paddingLeft(或paddingStart)与可绘制的单选按钮有关。同样适用于大约补丁九。为了更好地说明填充差异,请参阅附加的屏幕截图。

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).

如果您深入研究代码,您会发现 API 17 中有一个名为getHorizo​​ntalOffsetForDrawables的新方法。在计算单选按钮的左填充时调用此方法(因此图片中显示了额外的空间)。

TL;DR Just use paddingLeftif your minSdkVersionis >= 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+.

TL;DRpaddingLeft如果你minSdkVersion是 >= 17就使用。如果你支持 API <= 16,你应该有你支持的最小 SDK 的单选按钮样式和 API 17+ 的另一种样式。

combine screenshots showing left padding differences between API versions

合并显示 API 版本之间左侧填充差异的屏幕截图

回答by user438650

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

不确定这是否能解决您的问题,但您是否尝试过 Radio Button 的“Padding left”属性的值为 50dip 或更多

回答by Gianluca P.

i tried several ways and finished with this one working correctly on both emulator and devices:

我尝试了几种方法并完成了这个在模拟器和设备上都能正常工作的方法:

    <RadioButton
        android:background="@android:color/transparent"
        android:button="@null"
        android:drawableLeft="@drawable/your_own_selector"
        android:drawablePadding="@dimen/your_spacing" />
  • android:backgroundneeds to be transparent as it seems on api10 there is a background with intrinsic paddings (not tried with other apis but the solution works on others too)
  • android:buttonneeds to be nullas paddings will not work correctly otherwise
  • android:drawableLeftneeds to be specified instead of android:button
  • android:drawablePaddingis the space that will be between your drawable and your text
  • android:background需要是透明的,因为它在 api10 上似乎有一个带有内在填充的背景(未尝试使用其他 api,但该解决方案也适用于其他 api)
  • android:button需要为,否则填充将无法正常工作
  • 需要指定android:drawableLeft而不是android:button
  • android:drawablePadding是您的可绘制对象和文本之间的空间

回答by Hiren Patel

Add marginbetween a radiobuttonits labelby paddingLeft:

通过paddingLeft单选按钮标签之间添加边距

android:paddingLeft="10dip"

Just set your custom padding.

只需设置您的自定义填充

RadioButton's xml property.

RadioButton 的 xml 属性。

<RadioButton
    android:id="@+id/radHighest"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:button="@drawable/YourImageResource"
    android:drawablePadding="50dp"
    android:paddingLeft="10dip"
    android:text="@string/txt_my_text"
    android:textSize="12sp" />

Done

完毕

回答by Srikar Reddy

Use the following XML attributes. It worked for me

使用以下 XML 属性。它对我有用

For API <= 16 use

对于 API <= 16 使用

android:paddingLeft="16dp"

For API >= 17 use

对于 API >= 17 使用

android:paddingStart="@16dp"

Eg:

例如:

<android.support.v7.widget.AppCompatRadioButton
        android:id="@+id/popularityRadioButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:checked="true"
        android:paddingEnd="@dimen/radio_button_text"
        android:paddingLeft="@dimen/radio_button_text"
        android:paddingRight="@dimen/radio_button_text"
        android:paddingStart="@dimen/radio_button_text"
        android:text="Popularity"
        android:textSize="@dimen/sort_dialog_text_size"
        android:theme="@style/AppTheme.RadioButton" />

enter image description here

在此处输入图片说明

Further More: drawablePaddingattribute doesn't work. It only works if you added a drawable in your radio button. For Eg:

进一步:drawablePadding属性不起作用。它仅在您在单选按钮中添加可绘制对象时才有效。例如:

<RadioButton
    android:id="@+id/radioButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:button="@null"
    android:drawableEnd="@android:drawable/btn_radio"
    android:drawablePadding="56dp"
    android:drawableRight="@android:drawable/btn_radio"
    android:text="New RadioButton" />

回答by Mark B

Can't try this right now to verify, but have you tried to see if the attribute android:drawablePaddingdoes what you need?

现在无法尝试进行验证,但是您是否尝试过查看属性android:drawablePadding是否满足您的需求?

回答by Dilavar Malek

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

    checkBox.getPaddingTop(),
    checkBox.getPaddingRight(),
    checkBox.getPaddingBottom());

回答by Faakhir

The padding between the drawables and the text. It will be achieved by adding line below in xml file. android:drawablePadding="@dimen/10dp"

可绘制对象和文本之间的填充。这将通过在 xml 文件中添加以下行来实现。 android:drawablePadding="@dimen/10dp"

回答by David Guo

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”会起作用。paddingLeft 只会影响文本,同时保持无线电图像保持在原始位置。

回答by Lowerland

The "android:paddingLeft" only seems to work correctly under android 4.2.2

“android:paddingLeft”似乎只能在 android 4.2.2 下正常工作

i have tried almost all versions of android and it only works on the 4.2.2 version.

我已经尝试了几乎所有版本的 android,它只适用于 4.2.2 版本。