【问题标题】:Radio Buttons are aligned incorrectly单选按钮对齐不正确
【发布时间】:2013-01-23 01:54:16
【问题描述】:

请看下面的代码

    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".Form" >

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




            <TextView
               android:id="@+id/gender"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:text="Text To Be Displayed"
               android:layout_below="@+id/hipsTxt"
               /> 

            <RadioGroup 
                android:id="@+id/genderRadio"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                    android:layout_toRightOf="@+id/gender"
                    android:layout_alignBaseline="@+id/gender"
                >

                <RadioButton
                    android:id="@+id/male"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Male" />

                <RadioButton
                    android:id="@+id/female"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Female" />

            </RadioGroup>


        </RelativeLayout>

</ScrollView>

在这里,我需要在TextView 旁边显示单选按钮。但相反,它显示在文本视图下方!这里有什么问题?请帮忙!

PS:安卓版本 2.3.3

【问题讨论】:

    标签: java android eclipse radio-button radio-group


    【解决方案1】:
    <?xml version="1.0" encoding="utf-8"?>
    

        <RelativeLayout 
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            > 
    
            <RadioGroup 
                android:id="@+id/genderRadio"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                    android:layout_toRightOf="@+id/gender"
    
                >
    
                <RadioButton
                    android:id="@+id/male"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Male" />
    
                <RadioButton
                    android:id="@+id/female"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Female" />
    
            </RadioGroup>
    
            <TextView
                android:id="@+id/gender"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_centerVertical="true"
                android:text="Text To Be Displayed" />
    
        </RelativeLayout>
    

    尝试并更改了您的 xml 内容,现在它已正确对齐,请尝试使用这个。

    【讨论】:

    • 感谢您的帮助。我真的很感激:)
    【解决方案2】:

    代替

    android:layout_alignBaseline="@+id/gender"

    使用

    android:layout_alignParentTop="true"

    【讨论】:

    • 感谢您的回复。对此,我真的非常感激。来自我的 +1 :)
    【解决方案3】:

    试试这个:

    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".Form" >
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >
    
            <TextView
                android:id="@+id/gender"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:layout_weight="1"
                android:text="Text To Be Displayed" />
    
            <RadioGroup
                android:id="@+id/genderRadio"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:orientation="horizontal" >
    
                <RadioButton
                    android:id="@+id/male"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Male" />
    
                <RadioButton
                    android:id="@+id/female"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Female" />
            </RadioGroup>
        </LinearLayout>
    
    </ScrollView>
    

    【讨论】:

    • 感谢您的回复。对此,我真的非常感激。来自我的 +1 :)
    猜你喜欢
    • 2022-12-04
    • 1970-01-01
    • 2017-01-27
    • 2014-07-08
    • 2016-04-08
    • 2011-05-12
    • 2021-01-20
    • 1970-01-01
    • 2020-07-23
    相关资源
    最近更新 更多