我怎么能在radiogroup android里面对齐单选按钮?

问题描述 投票:0回答:3

我需要像RadioLayout一样将RadioButtons对齐在RadioGroup中。我已经读过RadioGroup继承自LinearLayout,并且我可能无法在其中对齐像RelativeLayout这样的内容。我想要实现的实际内容是RadioGroup内的两行,第一行包含两个RadioButtons,在第二行中,我必须在它的开头添加另一个按钮。我怎么能这样做?

android android-radiogroup
3个回答
2
投票

您需要做的就是将无线电组中的方向设置为水平以使它们水平对齐,然后查看下面的代码。

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:orientation="vertical"
    android:padding="@dimen/activity_horizontal_margin">


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

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="New RadioButton"
            android:id="@+id/radioButton" />

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="New RadioButton"
            android:id="@+id/radioButton2" />
    </RadioGroup>
    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New RadioButton"
        android:layout_gravity="start"
        android:id="@+id/radioButton3" />

</LinearLayout>

0
投票

你可以尝试这个布局,让我知道这是否适合你。如果需要,我可以修改它。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="vertical">

<RadioGroup
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="10dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="5dp">

        <RadioButton
            android:id="@+id/radioButton1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Red"/>

        <RadioButton
            android:id="@+id/radioButton2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Blue"/>
    </LinearLayout>

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

        <RadioButton
            android:id="@+id/radioButton3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Green"/>

    </LinearLayout>
</RadioGroup>

</LinearLayout>

Layout screenshot


0
投票

默认情况下,RadioGroup只识别RadioButtons作为直接子项添加并且像LinearLayout一样,但如果我们编写自己的RadioGroups,我们可以调整行为。

现在有很多项目,所以我们不需要重新发明:https://github.com/search?q=radiogroup。这是搜索结果的链接,除非Github死亡,否则不会无效。

我更喜欢this solution 它只是将RadioButtons检测为间接儿童。

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.