Как вы размещаете форму, которая прокручивается в Android SDK? - PullRequest
3 голосов
/ 02 декабря 2010

Мне нужно, чтобы пользовательская форма была больше экрана. Макет состоит из фиксированной «области заголовка», формы (возможно, в виде прокрутки) и кнопки внизу. Я создал три макета, а средний макет представляет собой ScrollView с вертикально ориентированной LinearLayout. К сожалению, форма отталкивает кнопки от экрана?!

Есть ли простой пример для этого?

Я полагаю, что я могу использовать ListView, но я думаю, что моим ScrollView легче управлять. Вот мой макет ...

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

    <!-- Title Area on top -->
    <LinearLayout
    android:id="@+id/layout_form"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    >
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
    >
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="0dip"
            android:layout_marginBottom="0dip"
            android:paddingTop="20dip"
            android:paddingBottom="20dip"
            android:paddingLeft="5dip"
            android:paddingRight="10dip"
            android:src="@drawable/logo"
            android:layout_gravity="center"
            android:layout_weight="1"
            />
        <TextView  
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:layout_gravity="center"
            android:layout_weight="2"
            android:layout_marginTop="0dip"
            android:layout_marginBottom="0dip"
            android:layout_marginLeft="4dip"
            android:layout_marginRight="4dip"
            android:text="@string/title_create"
            />
    </LinearLayout>

    <!-- Form entry on top -->

        <ScrollView 
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:fillViewport="true"
            android:clipChildren="true"
            >
            <LinearLayout 
                android:orientation="vertical"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
            >
        <TextView  
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content" 
            android:layout_gravity="center_horizontal"
            android:layout_marginLeft="8dip"
            android:layout_marginRight="8dip"
            android:text="@string/label_field_one"
            />
        <EditText
            android:id="@+id/edit_field_one"
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content" 
            android:singleLine="true"
            android:layout_marginLeft="10dip"
            android:layout_marginRight="10dip"
            android:hint="@string/hint_field_one"
            />
        <TextView  
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content" 
            android:layout_gravity="center_horizontal"
            android:layout_marginLeft="8dip"
            android:layout_marginRight="8dip"
            android:text="@string/label_field_two"
            />
        <EditText
            android:id="@+id/edit_field_two"
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content" 
            android:singleLine="true"
            android:layout_marginLeft="10dip"
            android:layout_marginRight="10dip"
            android:hint="@string/hint_field_two"
            />

            <!-- Repeat label-text and edit-text until done ... -->

            </LinearLayout>
            </ScrollView>


    <!-- "spring" between form and wizard buttons. -->
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
    />

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="left"
        android:layout_weight="2"
        >
        <Button
            android:id="@+id/btn_submit"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:layout_weight="1"
            android:text="@string/button_submit"
        />
    </LinearLayout>
        </LinearLayout>


</LinearLayout>

1 Ответ

3 голосов
/ 02 декабря 2010

Если я правильно понимаю, это то, что довольно часто упоминается - иметь фиксированный верхний и нижний колонтитулы и заполнять середину чем-либо (в вашем случае, ScrollView). Это то, что вы ищете? Если это так, вы можете попробовать что-то вроде ниже:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <LinearLayout
        android:id="@+id/header"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        >
        //.....stuff here
    </LinearLayout>
    <LinearLayout
        android:id="@+id/footer"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        >
        //.....stuff here
    </LinearLayout>
    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_above="@id/footer"
        android:layout_below="@id/header"
        >
        //......stuff here
    </ScrollView>
</RelativeLayout>

По сути, вы определяете заголовок, присваиваете ему фиксированную высоту (в данном случае wrap_content) и задаете его выравнивание по верху родительского элемента (RelativeLayout). Затем сделайте то же самое для нижнего колонтитула, совместив его с нижней частью родительского. Вы можете заменить только кнопку для LinearLayout для нижнего колонтитула, для вашей ситуации, или вы можете просто поместить кнопку внутри LinearLayout. Вероятно, немного быстрее накачать макет без дополнительного LinearLayout. Но в любом случае, затем, определите свою форму, область прокрутки. Установите высоту fill_parent и укажите, что он должен быть выше нижнего колонтитула и ниже заголовка. Это заставит его занимать все оставшееся пространство между верхним и нижним колонтитулом.

...