【问题标题】:Using RelativeLayout inside RadioGroup make RadioButtons not exclusive在 RadioGroup 中使用 RelativeLayout 使 RadioButtons 不独占
【发布时间】:2015-02-10 09:06:51
【问题描述】:

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

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

【问题讨论】:

    标签: android android-radiogroup android-radiobutton


    【解决方案1】:

    以前的答案包含一些问题。要选择的点不在一列中,它们未对齐。这是看起来与您的相同的解决方案。

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

    【讨论】:

    • 如果我使用它,按钮不会显示。如果我通过 match_parent 更改布局宽度换行内容,它们将显示在左侧,它们不在水平中心
    • 让我们试试我的新解决方案
    • RadioButton 扩展了 LinearLayout,其行为是相同的。如果你想做一些特别的事情,你必须添加额外的布局
    • 你试过我的新解决方案了吗?
    【解决方案2】:

    是的,您可以通过将 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>
    

    【讨论】:

    • 这样做的问题是它们在中心但没有被收音机对齐
    【解决方案3】:

    您好,请使用以下内容:

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

    【讨论】:

    • 它将 RadioButtons 放在顶部。我需要它们在水平和垂直的中心。他们也没有被他们的收音机对齐
    • 现在他们在中心,但没有被他们的收音机对齐:(
    • 尝试上面的sowi是用旧的:)
    猜你喜欢
    • 2011-08-29
    • 2018-04-17
    • 2012-05-05
    • 1970-01-01
    • 2011-09-13
    • 2011-07-03
    • 1970-01-01
    • 2012-05-12
    • 2013-06-14
    相关资源
    最近更新 更多