我正在尝试制作一个在水平和垂直中心有两个单选按钮的 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/