布局屏幕。需要工具栏留在底部

时间:2011-05-09 22:34:41

标签: android android-layout

我想将屏幕划分为2个垂直段。底部的工具栏应该是固定的 - 假设我想让LinearLayout无论如何都要保持在底部。

在顶部 - 我希望ScrollView会长大到工具栏,然后允许滚动。否则 - 它可能是完全空的,但工具栏仍然需要在底部。

我怎样才能做到这一点?

2 个答案:

答案 0 :(得分:7)

与往常一样,有几种方法。我会做以下

<?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="fill_parent"
        android:orientation="vertical">
        <ScrollView
            android:layout_width="fill_parent"
            android:layout_height="0dip"
            android:layout_weight="1">
        </ScrollView>
        <LinearLayout
            android:layout_height="wrap_content"
            android:layout_width="fill_parent">
        <Button
            android:text="Button1"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"/>
        <Button
            android:text="Button2"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"/>
        </LinearLayout>
</LinearLayout>

这使用垂直线性布局并使用wrap_content将按钮放在底部,然后通过赋予ScrollView权重“1”来为ScrollView提供剩余的空间。

答案 1 :(得分:0)

这是一种方法,关键是android:layout_alignParentBottom =“true”在第二个LinearLayout中,这里有两个按钮。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="fill_parent"
        android:layout_height="fill_parent" android:layout_marginTop="5dp"
        android:layout_marginBottom="60dp" android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp">

        <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:text="Some text" />

        </LinearLayout>
    </ScrollView>

    <LinearLayout android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:paddingTop="5dp"
        android:layout_alignParentBottom="true" android:layout_marginTop="5dp">

        <Button android:id="@+id/ok_button" android:layout_width="fill_parent"
            android:layout_weight="1" android:layout_height="wrap_content"
            android:text="OK" android:layout_alignParentLeft="true" />

        <Button android:id="@+id/cancel_button" android:layout_width="fill_parent"
            android:layout_weight="1" android:layout_height="wrap_content"
            android:text="Cancel" android:layout_toRightOf="@id/ok_button" />
    </LinearLayout>
</RelativeLayout>