【问题标题】:RadioGroup allows multiple RadioButtons to be selectedRadioGroup 允许选择多个 RadioButtons
【发布时间】:2013-06-14 00:11:43
【问题描述】:

我在 XML 中定义了一个 RadioGroup,它有两个 RadioButton。但是,我需要将标签显示在按钮本身的左侧,标签左对齐,按钮右对齐。为此,我使用了一个包含 TextView 和 RadioButton 的 RelativeLayout,没有 text。这大概是布局的样子:

<RadioGroup android:id="@+id/foo" android:layout_height="wrap_content" android:layout_width="match_parent">
    <RelativeLayout android:layout_height="wrap_content" android:layout_width="match_parent">
        <TextView android:text="@string/bar_one" android:layout_alignParentLeft="true" android:layout_height="wrap_content" android:layout_width="wrap_content" />
        <RadioButton android:id="@+id/bar1" android:layout_alignParentRight="true" android:layout_width="wrap_content" android:layout_height="wrap_content" />
    </RelativeLayout>
    ...
</RadioGroup>

这显示了我期望的方式,但我发现 RadioGroup 允许选择多个 RadioButton。当我删除RelativeLayouts和TextViews,所以RadioButtons嵌套在RadioGroup下直接,只能选择一个。有没有一种方法可以包装我的 RadioButtons 但同时防止多个单选按钮被选中?

如果没有,有没有更好的方法来完成我所追求的样式?

【问题讨论】:

  • @MaciejGórski 这个问题非常接近,尽管它只需要在按钮和标签之间进行一些填充,而不是让它们与相对侧对齐。我相信对齐它们需要在 RadioGroup 和 RadioButton 之间有一个 ViewGroup(这可以防止选择相互排斥),但我很好奇可能有哪些替代方案,如果有的话。

标签: android


【解决方案1】:

使用完整的 xml 会更容易分辨,因为您只显示一个 RadioButton,因此无法知道另一个的属性是什么,或者屏幕截图会有所帮助。但是您可以使用LinearLayout 并执行类似的操作

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
...>
    <RadioGroup
     ...>
     <TextView
      .../>
     <RadioButton
     .../>
    <TextView
      .../>
    <RadioButton
    .../>
</RadioGroup>
</LinearLayout

但在再次阅读问题后,我认为您可能想要类似的东西

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="wrap_content" 
    android:layout_width="match_parent">

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

        <RadioButton android:id="@+id/bar1" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" />

        <RadioButton
            android:id="@+id/bar2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </RadioGroup>

    <TextView android:id="@+id/textView1"
        android:text="Text 1" 
        android:layout_alignParentLeft="true" 
        android:layout_height="wrap_content" 
        android:layout_width="wrap_content"
        android:layout_alignBottom="@+id/bar1"/>

    <TextView android:text="Text 2" 
        android:layout_alignParentLeft="true" 
        android:layout_height="wrap_content" 
        android:layout_width="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_alignBottom="@+id/foo" />      
</RelativeLayout>

您可能需要调整一些 padding 和/或 margin 或进行其他调整才能获得您想要的效果,但我认为这会让您接近。如果我误解了,请告诉我

【讨论】:

    猜你喜欢
    • 2017-01-14
    • 1970-01-01
    • 2012-04-14
    • 2016-04-07
    • 2012-05-12
    • 1970-01-01
    • 2016-06-28
    • 2013-07-15
    相关资源
    最近更新 更多