【问题标题】:How do you layout a form that scrolls in Android SDK?如何布局在 Android SDK 中滚动的表单?
【发布时间】:2010-12-01 22:19:12
【问题描述】:

我需要一个比屏幕大的用户表单。布局由固定的“标题区域”、表单(可能在滚动视图中)和底部的按钮组成。我已经创建了三个布局,中间布局是一个带有垂直方向 LinearLayout 的 ScrollView。不幸的是,表单将按钮推离了屏幕?!

有没有一个简单的例子可以做到这一点?

我想我可以使用 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>

【问题讨论】:

    标签: android forms layout scrollview


    【解决方案1】:

    如果我理解正确,这是很常见的问题 - 有一个固定的页眉和页脚,并用任何东西填充中间(在你的情况下,一个 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,并告诉它位于页脚上方和页眉下方。这将导致它占用页眉和页脚之间的所有剩余空间。

    【讨论】:

    • 是的,我正在寻找带有在中间滚动的表单的页眉(说明性)/页脚(可操作)。我将立即编写代码,并让您知道这是否适用于我的布局。
    • 那行得通。但是现在输入法的行为不像我想要的那样(不同的问题,可能还有新的问题)。我没有得到 NEXT 按钮,并且当您触摸输入数据时,房地产太拥挤了。
    • 不动产问题只是屏幕尺寸的限制,真的(facebook 应用程序就是一个很好的(可怕的?)例子),但我相信你可以让键盘全屏打开,或将内容向上推以适合剩余区域内的 EditText。另外,我相信您必须在 EditText 框中指定它们应该为键盘提供“下一步”按钮,但不幸的是我自己并没有做太多这类事情,所以您可能想提出另一个问题为此。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-19
    • 2015-09-03
    • 2013-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多