在 Android 上带有边距的 RadioButton 左侧的文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12567950/
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 11:03:17  来源:igfitidea点击:

Text leftside of a RadioButton with a margin on Android

androidlayouttextviewradio-button

提问by Yalla T.

I want to have the "label", - the text you set in your RadioButtonto show left of the button and have some padding in between.

我想要“标签”, - 您在RadioButton按钮中设置的文本显示在按钮的左侧,并且中间有一些填充。

Adding an additional TextViewinto the layout doesn't work because my RadioGroupdoes not work (i can choose multiple buttons) if i add anything other then a RadioButtoninto the RadioGroup.

TextView在布局中添加其他内容不起作用,因为RadioGroup如果我RadioButtonRadioGroup.

So, how can i change the RadioButton to be <text><buttondrawable>instead of <buttondrawable><text>

那么,如何将 RadioButton 更改<text><buttondrawable><buttondrawable><text>

回答by Ameer Moaaviah

You can achieve this by setting android:button="@null"and adding the drawable of the RadioButtonas android:drawableRight. You can change the Padding between the text and the drawable with android:drawablePadding.

您可以通过设置android:button="@null"和添加RadioButtonas的 drawable来实现这一点android:drawableRight。您可以使用 更改文本和可绘制对象之间的填充android:drawablePadding

    <RadioGroup
        android:id="@+id/radios"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:inputType="text"
        android:orientation="vertical" >

        <RadioButton
            android:id="@+id/first"
            android:button="@null"
            android:drawableRight="@android:drawable/btn_radio"
            android:drawablePadding="20dp"
            android:layout_width="200dip"
            android:layout_height="wrap_content"
            android:text="@string/first"
            android:textSize="20dip" />

        <RadioButton
            android:id="@+id/second"
            android:button="@null"
            android:drawableRight="@android:drawable/btn_radio"
            android:drawablePadding="20dp"
            android:layout_width="200dip"
            android:layout_height="wrap_content"
            android:text="@string/second"
            android:textSize="20dip" />
    </RadioGroup>
</RelativeLayout>

回答by actsai

The answer above makes the image have no selected state.

上面的答案使图像没有选择状态。

May this help you:

可以帮助你:

  • On API <= 16 you can set padding left 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 left padding is in relation to the radio button drawable. Same applies to the about a patch nine. To better illustrate padding differences see the attached screen shot.

  • 在 API <= 16 上,您可以在单选按钮上设置 padding left 以设置相对于单选按钮视图边界的 padding。此外,补丁九背景也会改变相对于视图的视图边界。

  • 在 API 17 上,左内边距与可绘制的单选按钮有关。同样适用于大约补丁九。为了更好地说明填充差异,请参阅附加的屏幕截图。

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 You should have radio button style for the min sdk you are supporting and another style for api 17+

TL;DR 你应该为你支持的 min sdk 设置单选按钮样式,并为 api 17+ 设置另一种样式

the answer is from this same questionanswered by @James Baca

答案来自@James Baca 回答的同一个问题

回答by Ecaterina Galeru

you can also change layout direction right to left and align text to start

您还可以从右到左更改布局方向并对齐文本以开始

android:layoutDirection="rtl"
android:textAlignment="textStart"