【问题标题】:Radio Button in one radio group in 2 horizontal line2 条水平线中的一个单选组中的单选按钮
【发布时间】:2015-09-05 23:13:01
【问题描述】:

我尝试在 2 行中的一个 Radio 组中设置 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>

【问题讨论】:

  • 请您添加其余的布局吗?
  • 我在这里放了一张图片,我可以这样设置布局,但之后无线电组功能不起作用

标签: android android-layout radio-button radio-group


【解决方案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>

【讨论】:

    【解决方案2】:

    我不知道你是否还需要另一个选项,但你可以使用这个“强制”第二行:

    1. 设置方向 RadioGroup 水平
    2. 第二次在同一“行”中的radioButtons中设置相同的marginTop
    3. 第三个在每个(2 和向前)的第一个元素上设置一个负 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>
    

    【讨论】:

      【解决方案3】:

      RadioGroup 目前不允许嵌套布局。 (有关详细信息,请参阅 AOSP 问题 #8952)

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

      既然如此,并且注意到 RadioGroup 扩展了 LinearLayout,我认为您不得不在一行或一列中列出所有单选按钮。

      顺便说一句,没有什么可以阻止您创建自己的 RadioGroup 版本,该版本从像 RelativeLayout 这样更灵活的东西扩展而来。您可以从 RadioGroup 中的代码开始,并根据您的需要对其进行调整。

      【讨论】:

      • 那么如何处理从给定的四个选项中选择一个选项之类的功能呢?
      • 您可以通过对组中的每个单选按钮使用 setOnCheckedChangeListeners 以编程方式设置此互斥范围,当您看到检查更改时,取消选中逻辑组中的所有其他按钮。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-26
      • 2015-12-15
      相关资源
      最近更新 更多