在 RadioGroup 中使用 RelativeLayout 使 RadioButtons 不排他

Using RelativeLayout inside RadioGroup make RadioButtons not exclusive

我正在尝试制作一个在水平和垂直中心有两个单选按钮的 RadioGroup。但我也希望他们通过无线电保持对齐,因为他们有不同的文本长度。我使用了这段代码,但它们并不互斥:

      <RadioGroup
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:gravity="center">

            <RelativeLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center|center_vertical">

                <RadioButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/rbtn_gps_to"
                    android:text="to"
                    android:checked="true"
                    />


                <RadioButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/rbtn_gps_from"
                    android:text="from"
                    android:layout_below="@+id/rbtn_gps_to"
                    />


            </RelativeLayout>


        </RadioGroup>

它有效,两者都按照我的意愿对齐,但问题是当我检查一个然后检查另一个时,第一个也保持选中状态,所以它们不是排他的。当我删除相对布局时,单选按钮是独占的。有什么方法可以创建我想要的布局并且单选按钮保持排他性吗?

您好,请使用以下内容:

<RadioGroup
    android:id="@+id/radio_group_1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"

    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <RadioButton

        android:id="@+id/rbtn_gps_from"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingRight="20dp"
        android:text="from" />

    <RadioButton
        android:id="@+id/rbtn_gps_to"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingRight="20dp"
        android:text="to" />
</RadioGroup>

是的,您可以通过将 radioGroup 的 xml 属性 设置为垂直来实现您需要的布局。

 <RadioGroup
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:gravity="center">

            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/rbtn_gps_to"
                android:text="to"
                android:checked="true"
                />


            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/rbtn_gps_from"
                android:text="from"

                />

    </RadioGroup>

之前的回答有一些问题。要选择的点不在一列中,它们没有对齐。这是看起来和你一样的解决方案。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
    <RadioGroup
            android:layout_width=" wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:orientation="vertical">
        <RadioButton android:id="@+id/rbtn_gps_to"
                     android:layout_width="wrap_content"
                     android:layout_height="wrap_content"
                     android:text="to"/>
        <RadioButton android:id="@+id/rbtn_gps_from"
                     android:layout_width="wrap_content"
                     android:layout_height="wrap_content"
                     android:text="from"/>
    </RadioGroup>
</RelativeLayout>