android - 在 RadioGroup 中使用 RelativeLayout 使 RadioButtons 不排他

标签 android android-radiogroup android-radiobutton

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

      <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>

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

最佳答案

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

<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>

关于android - 在 RadioGroup 中使用 RelativeLayout 使 RadioButtons 不排他,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28427716/

相关文章:

Android 和 Google 电子钱包 - 一次性购买是强制性的吗?

java - 如何理解 Android 中的电话忙?

android - 找不到 toLeftOf 资源

java - 导航回可重用 Activity

Android:单选按钮不是独占的

android - RadioButton 在取消选中后留下黑色选中的圆圈

android - 居中 RadioGroup 中 RadioButton 的图标

android - 如何找出选定的单选选项,当 android :radiobutton ="@null" in android?

安卓。 ImageView 作为 RadioButton

android - 单击列表项时如何将单选按钮设置为选中