Relativelayout inside Scrollview not working

2019-01-26 06:16发布

问题:

I have a relative layout inside a scrollview and I can't get it to work.I tried setting my relative layout's height to wrap_content but it doesn't work also. I can get it to work with linear layout but I don't want to use it for some reason.

Here is my xml:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:fillViewport="true">

<RelativeLayout
    android:id="@+id/container"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#FFFFFF"
    android:orientation="vertical"
    android:padding="14dp">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="48dp"
        android:inputType="textPersonName"
        android:id="@id/feedback_name"
        android:hint="Name"
        android:textColor="#000000"
        android:layout_alignParentTop="true"
        android:ems="10"
        android:maxHeight="48dp"
        android:textSize="14sp" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="48dp"
        android:inputType="textEmailAddress"
        android:ems="10"
        android:maxHeight="48dp"
        android:id="@id/feedback_email"
        android:hint="Your email (optional)"
        android:textColor="#000000"
        android:layout_below="@id/feedback_name"
        android:textSize="14sp" />

    <Spinner
        android:id="@id/feedback_concern"
        android:layout_width="match_parent"
        android:layout_height="48dp"
        android:minHeight="48dp"
        android:layout_below="@id/feedback_email" />

    <EditText
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:inputType="textMultiLine"
            android:ems="10"
            android:id="@id/feedback_message"
            android:hint="Message"
            android:textColor="#000000"
        android:layout_weight="1"
        android:minHeight="70dp"
            android:gravity="top"
            android:textSize="14sp"
            android:layout_below="@id/feedback_concern"
        android:layout_above="@id/feedback_send"
/>


    <Button
        android:layout_width="match_parent"
        android:layout_height="48dp"
        android:text="Send"
        android:maxHeight="48dp"
        android:id="@id/feedback_send"
        android:background="@color/light"
        android:textColor="#FFFFFF"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <ProgressBar
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/progressBar"
        android:visibility="invisible"
        android:layout_gravity="center_horizontal"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="error"
        android:fontFamily="sans-serif-light"
        android:id="@id/errorText"
        android:visibility="invisible"
        android:layout_gravity="center_horizontal"
        android:textColor="@android:color/black"
        android:layout_below="@id/feedback_message"
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true"
        android:gravity="center"
        android:singleLine="true" />

</RelativeLayout>
</ScrollView>

Portrait:

Landscape:

You can see that the message edittext disappeared when I go to landscape mode and the layout doesn't scroll

回答1:

Apply the scrollview from top to message edittext.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#FFFFFF"
android:orientation="vertical"
android:padding="14dp" >

<ScrollView
    android:id="@+id/scroll"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_above="@+id/feedback_send"
    android:fillViewport="true" >

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

        <EditText
            android:id="@+id/feedback_name"
            android:layout_width="fill_parent"
            android:layout_height="48dp"
            android:layout_alignParentTop="true"
            android:ems="10"
            android:hint="Name"
            android:inputType="textPersonName"
            android:maxHeight="48dp"
            android:textColor="#000000"
            android:textSize="14sp" />

        <EditText
            android:id="@+id/feedback_email"
            android:layout_width="fill_parent"
            android:layout_height="48dp"
            android:layout_below="@id/feedback_name"
            android:ems="10"
            android:hint="Your email (optional)"
            android:inputType="textEmailAddress"
            android:maxHeight="48dp"
            android:textColor="#000000"
            android:textSize="14sp" />

        <Spinner
            android:id="@+id/feedback_concern"
            android:layout_width="fill_parent"
            android:layout_height="48dp"
            android:layout_below="@id/feedback_email"
            android:minHeight="48dp" />

        <EditText
            android:id="@+id/feedback_message"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/feedback_concern"
            android:ems="10"
            android:gravity="top"
            android:hint="Message"
            android:inputType="textMultiLine"
            android:minHeight="70dp"
            android:textColor="#000000"
            android:textSize="14sp" />
    </RelativeLayout>
</ScrollView>

<Button
    android:id="@id/feedback_send"
    android:layout_width="fill_parent"
    android:layout_height="48dp"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:background="@android:color/background_dark"
    android:maxHeight="48dp"
    android:text="Send"
    android:textColor="#FFFFFF" />

<ProgressBar
    android:id="@+id/progressBar"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_gravity="center_horizontal"
    android:visibility="invisible" />

<TextView
    android:id="@+id/errorText"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_below="@+id/scroll"
    android:layout_centerHorizontal="true"
    android:layout_gravity="center_horizontal"
    android:gravity="center"
    android:singleLine="true"
    android:text="error"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:textColor="@android:color/black"
    android:visibility="invisible" />

</RelativeLayout>


回答2:

In order to get this down use the following:

<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"
    android:fillViewport="true">


    <RelativeLayout
        android:id="@+id/container"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">

    (all your view elements)

    </RelativeLayout>
</ScrollView>

No need for extra layouts tweeks. :-)