安卓RelativeLayout对齐问题

6
我正在处理Android布局并尝试制作一个简单的QA测试,但我无法正确排列这些元素,经过几个小时的努力后,我需要一点帮助。
这是我想要的: enter image description here 而这是我得到的: enter image description here 以下是我的XML代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rootLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<ScrollView
    android:id="@+id/scrollView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true" >

    <RelativeLayout
        android:id="@+id/nodeLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <TextView
            android:id="@+id/textQuestion"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:text="TextView"
            android:textSize="18dp" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:orientation="vertical" >

            <RadioGroup
                android:id="@+id/radioAnswersGroup"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >
            </RadioGroup>

            <LinearLayout
                android:id="@+id/questionsNavigationGroup"
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:orientation="horizontal" >

                <Button
                    android:id="@+id/buttonPreviousQuestion"
                    android:layout_width="match_parent"
                    android:layout_height="45dp"
                    android:layout_weight="1"
                    android:background="@drawable/fancy_button"
                    android:gravity="center|center_vertical"
                    android:onClick="onPreviousQuestionButtonClick"
                    android:shadowColor="#fff"
                    android:shadowRadius="3"
                    android:text="Back"
                    android:textColor="#432f11"
                    android:textSize="24dp" />

                <Button
                    android:id="@+id/buttonNextQuestion"
                    android:layout_width="match_parent"
                    android:layout_height="45dp"
                    android:layout_weight="1"
                    android:background="@drawable/fancy_button"
                    android:gravity="center|center_vertical"
                    android:onClick="onNextQuestionButtonClick"
                    android:shadowColor="#fff"
                    android:shadowRadius="3"
                    android:text="Next"
                    android:textColor="#432f11"
                    android:textSize="24dp" />

                <Spinner
                    android:id="@+id/spinner"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_weight="1" />
            </LinearLayout>

        </LinearLayout>

    </RelativeLayout>

</ScrollView>

</LinearLayout>

答案和按钮应始终位于底部,问题应始终位于顶部。但是当文本很长时,视图应在滚动视图中进行拉伸。

编辑:

我找到了一种不使用相对布局的方法。我不知道这是一个错误还是特性,但是此布局可以按照我的要求进行缩放。

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textQuestion"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="top" />

    <LinearLayout
        android:id="@+id/bottomStuff"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="bottom"
        android:orientation="vertical" >

        <RadioGroup
            android:id="@+id/radioAnswersGroup"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
        </RadioGroup>

        <LinearLayout
            android:id="@+id/questionsNavigationGroup"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:orientation="horizontal" >

            <Button
                android:id="@+id/buttonPreviousQuestion"
                android:layout_width="match_parent"
                android:layout_height="45dp"
                android:layout_weight="1" />

            <Button
                android:id="@+id/buttonNextQuestion"
                android:layout_width="match_parent"
                android:layout_height="45dp"
                android:layout_weight="1" />

            <Spinner
                android:id="@+id/spinner"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1" />

        </LinearLayout>

    </LinearLayout>

</LinearLayout>

</ScrollView>

你好,你用什么工具创建了这个屏幕布局?[http://i.stack.imgur.com/32wzH.png] - R.daneel.olivaw
你已经将整个布局都放在了ScrollView中,为什么不只是将TextView放在ScrollView中呢?这样问题的文本就可以滚动了:<scrollView>..<textView/>..</scrollView>。 - MKJParekh
我刚刚使用了Paint.NET来绘制这张图片。而我为什么不只是使用ScrollView来显示文本呢?因为如果问题太长,我希望它可以占据整个空间。 - Sylwester Kardziejonek
9个回答

2
我认为你应该使用这个:

我认为你应该使用这个:

<ScrollView
android:id="@+id/scrollView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true" >

1
将此行添加到您的TextView元素中:
android:layout_above="@+id/radioAnswersGroup"

0
你可以尝试给下面的 TextViewLinearLayout 添加一个 id,例如 android:id="@+id/bottomhalf",并且定义 android:layout_below="@+id/textQuestion"
然后在你的 TextView 中添加 android:layout_above="@+id/bottomhalf"

0
这将会起作用:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">

<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:id="@+id/linear1">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="text"/>
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:id="@+id/linear2">

    <RadioGroup android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/radioGroup1">
        <RadioButton
            android:layout_width="wrap_content"
            android:id="@+id/radio0"
            android:layout_height="wrap_content"
            android:text="RadioButton"
            android:checked="true" />

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

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

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

    </RadioGroup>

</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:id="@+id/linear3">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button1"
        android:text="Button" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button2"
        android:text="Button" />

    <Spinner android:id="@+id/spinner1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

</LinearLayout>
</ScrollView>

0

实际上,我一直在开发一个类似的项目,有与您相似的要求。我在面板上拥有旋转器、按钮、图像视图等。

请检查下面的 XML 布局代码,并尝试更改其中的一些参数以相应地进行调整。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="3sp" >

<LinearLayout
    android:id="@+id/linearLayout"
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:baselineAligned="true"
    android:orientation="horizontal" >

    <ScrollView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1" >

        <RelativeLayout
            android:id="@+id/innerRelativeLayout"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" >

            <ImageView
                android:id="@+id/imgStartStop"
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="20dp"
                android:contentDescription="@string/startstop"
                android:onClick="startstop"
                android:src="@drawable/start" />

            <Button
                android:id="@+id/btnStatus"
                android:layout_width="220dp"
                android:layout_height="75dp"
                android:layout_marginLeft="5dp"
                android:layout_marginRight="5dp"
                android:layout_marginTop="10dp"
                android:layout_toRightOf="@id/imgStartStop"
                android:onClick="showStatus"
                android:text="@string/ontime" />

            <ImageView
                android:id="@+id/imgPushToTalk"
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:layout_below="@id/imgStartStop"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="25dp"
                android:contentDescription="@string/pushtotalk"
                android:onClick="activatePushtoTalk"
                android:src="@drawable/pushtotalkidle" />

            <TextView
                android:id="@+id/txtExpectedNumberofPassengers"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/imgPushToTalk"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="5dp"
                android:text="@string/expectednumberofpassengers"
                android:textSize="30sp" />

            <TextView
                android:id="@+id/txtNumberofPassengers"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/imgPushToTalk"
                android:layout_marginTop="5dp"
                android:layout_toLeftOf="@id/txtExpectedNumberofPassengers"
                android:text="@string/numberofpassengers"
                android:textColor="@color/greenText"
                android:textSize="30sp" />
        </RelativeLayout>
    </ScrollView>

    <fragment
        android:id="@+id/mapFragment"
        android:name="com.google.android.gms.maps.MapFragment"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="3" />
</LinearLayout>

<TextView
    android:id="@+id/txtTickerMessage"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:ellipsize="marquee"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:marqueeRepeatLimit="marquee_forever"
    android:scrollHorizontally="true"
    android:singleLine="true"
    android:text="@string/tickermessage"
    android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>

0

试试这个

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="@+id/RelativeLayout1"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">

<ScrollView android:layout_width="wrap_content"
    android:layout_height="wrap_content" android:id="@+id/scrollView1"
    android:layout_alignParentTop="true" android:layout_alignParentLeft="true"
    android:layout_above="@+id/radioGroup1"
    android:layout_alignParentRight="true">

    <LinearLayout android:id="@+id/linearLayout1"
        android:layout_width="fill_parent" android:layout_height="fill_parent">
        <TextView android:textAppearance="?android:attr/textAppearanceLarge"
            android:layout_width="wrap_content" android:id="@+id/textView1"
            android:text="TextView" android:layout_height="wrap_content" />
    </LinearLayout>

</ScrollView>

<RadioGroup android:layout_width="wrap_content"
    android:layout_height="wrap_content" android:id="@+id/radioGroup1"
    android:layout_above="@+id/spinner1" android:layout_alignParentRight="true"
    android:layout_alignParentLeft="true">
    <RadioButton android:layout_width="wrap_content"
        android:id="@+id/radio0" android:layout_height="wrap_content"
        android:text="RadioButton" android:checked="true" />
    <RadioButton android:layout_width="wrap_content"
        android:id="@+id/radio1" android:layout_height="wrap_content"
        android:text="RadioButton" />
    <RadioButton android:layout_width="wrap_content"
        android:id="@+id/radio2" android:layout_height="wrap_content"
        android:text="RadioButton" />
    <RadioButton android:layout_width="wrap_content"
        android:id="@+id/radio3" android:layout_height="wrap_content"
        android:text="RadioButton" />
</RadioGroup>

<Button android:layout_width="wrap_content"
    android:layout_height="wrap_content" android:id="@+id/button1"
    android:text="Button" android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true" />
<Button android:layout_width="wrap_content"
    android:layout_height="wrap_content" android:id="@+id/button2"
    android:text="Button" android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true" />
<Spinner android:id="@+id/spinner1" android:layout_width="wrap_content"
    android:layout_height="wrap_content" android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true" />

</RelativeLayout>

0

我认为以下更改将解决问题,

<ScrollView
android:id="@+id/scrollView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true" >

而不是

<ScrollView
android:id="@+id/scrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true" >

希望它有所帮助。


0
请使用以下布局,它会产生你在第一个链接中提到的输出。根据间距和可绘制对象进行修改。希望能对你有所帮助。
我使用800X480纵向分辨率来检查输出,你需要根据你的设备分辨率修改布局的宽度和高度。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rootLayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ScrollView
        android:id="@+id/scrollView"
        android:layout_width="match_parent"
        android:layout_height="300px"
        android:fillViewport="true" >

        <TextView
            android:id="@+id/textQuestion"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_alignParentTop="true"
            android:text="TextView"
            android:textSize="18dp" />
    </ScrollView>
    <RadioGroup
        android:id="@+id/radioAnswersGroup"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true">
        <RadioButton android:layout_width="wrap_content"
            android:id="@+id/radio0" android:layout_height="wrap_content"
            android:text="RadioButton" android:checked="true" />
        <RadioButton android:layout_width="wrap_content"
            android:id="@+id/radio1" android:layout_height="wrap_content"
            android:text="RadioButton" />
        <RadioButton android:layout_width="wrap_content"
            android:id="@+id/radio2" android:layout_height="wrap_content"
            android:text="RadioButton" />
        <RadioButton android:layout_width="wrap_content"
            android:id="@+id/radio3" android:layout_height="wrap_content"
            android:text="RadioButton" />               
    </RadioGroup>

    <LinearLayout
        android:id="@+id/questionsNavigationGroup"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/buttonPreviousQuestion"
            android:layout_width="match_parent"
            android:layout_height="45dp"
            android:layout_weight="1"
            android:gravity="center|center_vertical"
            android:onClick="onPreviousQuestionButtonClick"
            android:shadowColor="#fff"
            android:shadowRadius="3"
            android:text="Back"
            android:textColor="#432f11"
            android:textSize="24dp" />

        <Button
            android:id="@+id/buttonNextQuestion"
            android:layout_width="match_parent"
            android:layout_height="45dp"
            android:layout_weight="1"                    
            android:gravity="center|center_vertical"
            android:onClick="onNextQuestionButtonClick"
            android:shadowColor="#fff"
            android:shadowRadius="3"
            android:text="Next"
            android:textColor="#432f11"
            android:textSize="24dp" />
        <Spinner
            android:id="@+id/spinner"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1" />
    </LinearLayout>

</LinearLayout>

0

我找到了一种不需要相对布局的方法。我不知道这是一个错误还是特性,但这个布局可以按照我想要的方式进行缩放。

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textQuestion"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="top" />

    <LinearLayout
        android:id="@+id/bottomStuff"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="bottom"
        android:orientation="vertical" >

        <RadioGroup
            android:id="@+id/radioAnswersGroup"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
        </RadioGroup>

        <LinearLayout
            android:id="@+id/questionsNavigationGroup"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:orientation="horizontal" >

            <Button
                android:id="@+id/buttonPreviousQuestion"
                android:layout_width="match_parent"
                android:layout_height="45dp"
                android:layout_weight="1" />

            <Button
                android:id="@+id/buttonNextQuestion"
                android:layout_width="match_parent"
                android:layout_height="45dp"
                android:layout_weight="1" />

            <Spinner
                android:id="@+id/spinner"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1" />

        </LinearLayout>

    </LinearLayout>

</LinearLayout>

</ScrollView>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接