Android RelativeLayout выровнять озабоченность - PullRequest
6 голосов
/ 17 декабря 2011

Я играю с макетами 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>

Ответы и кнопки должны быть всегда внизу, а вопрос должен быть всегда сверху. Но когда текст длинный, вид должен растягиваться внутри вида прокрутки.

EDIT:

Я нашел путь без относительного расположения. Я не знаю, является ли это ошибкой или функцией, но этот макет масштабируется так, как я хочу.

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

Ответы [ 9 ]

2 голосов
/ 18 марта 2013

Я думаю, вы должны использовать это:

<ScrollView
android:id="@+id/scrollView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true" >
1 голос
/ 17 декабря 2011

добавить эту строку в ваш элемент TextView:

android:layout_above="@+id/radioAnswersGroup"
0 голосов
/ 27 апреля 2015

Это будет работать:

<?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 голосов
/ 09 августа 2013

На самом деле я работаю над похожим проектом, имеющим некоторые аналогичные ваши требования.У меня есть и счетчик, и кнопки просмотра изображений на панели

Пожалуйста, проверьте приведенный ниже код макета 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 голосов
/ 22 декабря 2011

Я нашел путь без относительного расположения.Я не знаю, является ли это ошибкой или функцией, но этот макет масштабируется так, как я хочу.

<?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>
0 голосов
/ 17 декабря 2011

Используйте следующую раскладку, она производит вывод, как вы упоминали в 1-й ссылке. Изменить для пробелов и рисования. Я надеюсь, что это поможет вам.

Я использовал разрешение 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 голосов
/ 17 декабря 2011

Я думаю, что следующие изменения решат проблему,

<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 голосов
/ 17 декабря 2011

Вы можете попробовать присвоить LinearLayout ниже TextView идентификатор, такой как android:id="@+id/bottomhalf", а также определить android:layout_below="@+id/textQuestion"

Затем к вашему TextView добавьте android:layout_above="@+id/bottomhalf"

0 голосов
/ 17 декабря 2011

Попробуйте это

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