在两个水平线上的一个单选按钮组中的单选按钮

6

“输入图像描述”src

我试图在两行中的一个单选按钮组中设置4个单选按钮,但问题是当我采用水平方向的线性布局时,单选按钮组功能无法正常工作。所有单选按钮都被选择。一次只能选择一个按钮。

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" >

                <RadioButton
                    android:id="@+id/r1"

                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="@string/lbl1" />

                <RadioButton
                    android:id="@+id/r2"

                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="@string/lbl2" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" >

                <RadioButton
                    android:id="@+id/r3"

                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="@string/lbl3" />

                <RadioButton
                    android:id="@+id/r4"

                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="@string/lbl4" />
            </LinearLayout>
        </RadioGroup>

请问您能否添加剩余的布局呢? - Michael Krause
我在这里放了一张图片,我可以通过这种方式设置布局,但是之后单选按钮组的功能就无法使用了。 - Shivang Trivedi
3个回答

5

目前RadioGroup不支持嵌套布局(更多细节请参见AOSP问题#8952)。

因此,RadioButton必须是父级RadioGroup的直接子项。

考虑到这一点,并注意到RadioGroup扩展了LinearLayout,我认为你必须在一行或一列中列出所有的单选按钮。

顺便说一句,你可以创建自己的RadioGroup版本,它扩展自更灵活的RelativeLayout之类的东西。你可以从RadioGroup中的代码开始,并根据需要进行调整。


1
那么如何处理像从给定的四个选项中选择一个选项这样的功能呢? - Shivang Trivedi
1
您可以通过为组中的每个单选按钮设置setOnCheckedChangeListeners来编程设置此互斥作用域,并在检测到选中状态更改时取消选中逻辑组中的所有其他单选按钮。 - Michael Krause

0

我不知道你是否需要另一个选项,但你可以使用以下方法“强制”换行:

  1. 将 RadioGroup 的 orientation 设置为 horizontal
  2. 在同一“行”中的 radioButtons 中设置相同的 marginTop
  3. 对于每个(第二个及其后面的)第一个元素设置负的 marginStart 这将标记每行的开头,其他元素会跟随它

这是一个示例:

 <RadioGroup
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:orientation="horizontal"
  android:id="@+id/Rgroup">

 <RadioButton
   android:id="@+id/r1"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="@string/lbl1" />

   <RadioButton
   android:id="@+id/r2"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="@string/lbl2" />

  <RadioButton
   android:id="@+id/r3"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_marginStart="-180dp"
   android:layout_marginTop="40dp" 
   android:text="@string/lbl3" />

  <RadioButton
   android:id="@+id/r4"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_marginStart="0dp"
   android:layout_marginTop="40dp" 
   android:text="@string/lbl4" />

   </RadioGroup>

-1
要使RadioGroup具有列,只需在其中添加GridLayout并更改android:columnCount参数。 但是,您必须覆盖所有单选按钮的OnCheckedChangeListeners,因为当您将RadioButtons放入GridLayout中时,它们不再是一组。
<RadioGroup

        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">

        <GridLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:columnCount="3">

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

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

        .....


        </GridLayout>

    </RadioGroup>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接