【问题标题】:2 x 2 layout for a radio group android studio广播组 android studio 的 2 x 2 布局
【发布时间】:2018-04-12 18:49:58
【问题描述】:

我是 android studio 的新手,正在尝试为一些单选按钮设置 2 x 2 布局。我在网上看过,他们是类似的问题,所以很抱歉重复了一点,但无论我做什么,我似乎都无法让我的工作。这就是我的 xml 文件中的内容。

  <RadioGroup
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/radioGroup">

       <TableRow>

            <RadioButton
                android:id="@+id/radio1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:checked="true"
                android:text="@string/text1" />

            <RadioButton
                android:id="@+id/radio2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/text2" />
        </TableRow>
        <TableRow>
            <RadioButton
                android:id="@+id/radio3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/text3"/>

            <RadioButton
                android:id="@+id/radio4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/text4"/>
        </TableRow>
    </RadioGroup>

谁能解释为什么这不起作用?它只是让我选择所有按钮。

【问题讨论】:

    标签: android android-studio radio-button


    【解决方案1】:

    Radio Group 本身就是一个线性布局。

    单选组中的“方向”属性允许您水平或垂直对齐单选按钮(如线性布局)。您的 xml 中不需要单独的线性布局。

    但是,在您的情况下,因为当您尝试以网格方式设计它们时, 你不能做 Radio 组的互斥逻辑

    你也可以参考这个答案 How to group a 3x3 grid of radio buttons?

    【讨论】:

    • 感谢您的帮助。我更新了代码以使用 就像您链接我的示例一样,但仍然没有运气。有没有办法可以通过这种方式实现互斥?
    • 我可以建议的一件事是,您编写逻辑来根据选中状态检查/取消选中单个单选按钮..
    • 对不起,我不明白你的意思?我认为无线电组的全部意义在于一次只能检查一个,我认为它会自动执行此操作不?
    • 是的,radio group 的基本功能是自己处理互斥。但是,因为您希望以网格方式(这是您的自定义 UI)放置单选选项,所以该功能始终必须是自定义的,因为单选组无法处理它。如果您只是将所有选项水平/垂直放置在 radio group 中,则互斥由 radio group 处理。
    • 哦,我现在明白了,非常感谢。你有什么建议吗?对不起,我是一个完整的初学者
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-17
    • 1970-01-01
    • 1970-01-01
    • 2017-01-07
    • 2016-01-25
    • 1970-01-01
    相关资源
    最近更新 更多