简体   繁体   中英

Relative layout within Linear layout is scrollview possible?

I have a layout for entering a country into a database in my app. I would like my layout to be scrollable because on some devices the keyboard can end up blocking text fields. I know that I can use ScrollView to do this but I am having a problem. My layout consists of an overall LinearLayout but I have included 2 buttons within this in a RelativeLayout. This is giving me an unusual method of scrolling where the whole screen seems to scroll even when I am not entering in text, leaving a big gap at the bottom of the screen. Any thoughts on this?

Here is my layout:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    >
    <LinearLayout 
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:background="@drawable/map" 
        android:scrollbars = "vertical" >

        <!-- Enter country -->
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:hint="@string/editcountry"
            android:textColor="@color/black"
            android:textSize="22sp" />

        <AutoCompleteTextView 
            android:id="@+id/editcountry"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" 
            android:hint="@string/hintprompt"
            android:imeOptions="actionDone" />

        <!-- Enter year -->
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:hint="@string/edityear"
            android:textColor="@color/black"
            android:textSize="22sp" />

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

        <!-- Enter month -->
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:hint="@string/editmonth"
            android:textColor="@color/black"
            android:textSize="22sp" />

        <Spinner
            android:id="@+id/monthspin"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:entries="@array/months" />

        <!-- Enter mode of transport -->
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:hint="@string/edittransport"
            android:textColor="@color/black"
            android:textSize="22sp" />

        <Spinner
            android:id="@+id/transportspin"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:entries="@array/transport" />

        <!-- Enter description -->
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:hint="@string/editdesc"
            android:textColor="@color/black"
            android:textSize="22sp" />

        <EditText
            android:id="@+id/editdesc"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:hint="@string/hintprompt"
            android:inputType="text" 
            android:imeOptions="actionDone"/>

            <!-- Place buttons at the bottom of the screen -->
            <RelativeLayout 
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:orientation="vertical" >

                <Button
                    android:id="@+id/backmain"
                    android:layout_width="150dp"
                    android:layout_height="50dp"
                    android:layout_alignParentBottom="true"
                    android:layout_alignParentLeft="true"
                    android:text="@string/back" />

                <Button
                    android:id="@+id/add"
                    android:layout_width="150dp"
                    android:layout_height="50dp"
                    android:layout_alignParentBottom="true"
                    android:layout_alignParentRight="true"
                    android:text="@string/addinfo" />

            </RelativeLayout>
    </LinearLayout>
</ScrollView>

Your LinearLayout has it's height set to fill_parent . Change that to wrap_content . Your RelativeLayout is set the same way. Could also be the culprit.

<LinearLayout 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:background="@drawable/map" 
        android:scrollbars = "vertical" >

You can also put your scrollView inside another layout. Have one vertical LinearLayout with two Layouts inside, one for your everything that should scroll with layout_weight="1" and at the bottom another layout that consist of your buttons

Your layout will be something similar to this:

Vertical LinearLayout
    LinearLayout with layout_weight="1"
        ScrollView
            Layout containing your stuff
    layout containing buttons

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM