ScrollView和LinearLayout的困难

9

我正在尝试制作一个Android布局:垂直LinearLayout中包含3个组件。 中心组件是包含TextView的ScrollView。 当TextView包含大量文本(超出屏幕可显示范围时),ScrollView将延伸到屏幕底部,显示滚动条,并将最后一个组件-包含Button的LinearLayout推出屏幕。

如果ScrollView中的TextView内容足够短,则屏幕底部的按钮位置完美。

我要实现的布局如下图所示:

我编写的布局XML如下:

<?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">

    <TextView android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#FFFFFF"
            android:layout_marginLeft="10dip"
            android:layout_marginRight="10dip"
            android:layout_marginTop="10dip"
            android:layout_marginBottom="10dip"
            android:text="Title />

    <ScrollView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content">

        <TextView android:id="@+id/text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:autoLink="web"
                android:textColor="#FFFFFF"
                android:background="#444444"
                android:padding="10dip" />

    </ScrollView>

    <LinearLayout
            android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1">

        <LinearLayout
                android:orientation="horizontal"
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_weight="1"/>

        <Button android:id="@+id/login_button"
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_gravity="bottom"
                android:layout_weight="1"
                android:text="@string/next_button"/>

    </LinearLayout>

</LinearLayout>
4个回答

7
滚动视图是第二个视图对象,并设置为wrap_content,这比屏幕更大。我建议使用RelativeLayout。首先使用android:alignParentTop="true"设置顶部文本视图,然后使用android:alignParentBottom="true"设置底部LinearLayout,最后使用值android:alignBelow="@id/whatYouCallTheHeader将滚动视图列在xml的最后一个位置。这样可以将底部工具栏与屏幕底部对齐,将标题放置在顶部,不管大小如何。然后滚动视图将有自己的位置,在页眉和页脚放置后。

如果你的目标是1.6版本,它只会通过XML进行一次遍历,因此任何引用都必须在被引用之前布置好。2.1及以上版本则需要进行两次遍历。 - Phobos

2
尝试在 ScrollView 中添加布局权重,例如:
<ScrollView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1">

这对我来说是有效的,因为我的情况几乎与你提出的情况完全相同,但它让我感到困惑,因为增加控件的布局权重从0(如果您不指定 layout_weight,则默认为0)增加到1应该会使已经使用过多空间的控件变小,这是违反直觉的。

我认为它能够起作用的原因是,通过不指定 layout_weight,实际上允许布局忽略滚动视图大小与其他控件的相对大小关系,反之,如果您指定一个权重,您就允许它按比例缩小。


2

相对布局(RelativeLayout)比线性布局(LinearLayout)更适合您的需求。您可以使用一些属性,如alignBelow等。


2

![固定页眉-页脚和可滚动主体布局][1]


这就是你要找的。大多数安卓应用程序都采用这种布局,即固定页眉和页脚以及可滚动主体。此布局的XML代码为:


<?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:background="#5599DD"
        android:layout_height="fill_parent">
        <!-- Header goes here -->
       <TextView 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#FFFFFF"
            android:layout_marginLeft="10dip"
            android:layout_marginRight="10dip"
            android:layout_marginTop="10dip"
            android:layout_marginBottom="10dip"
            android:textSize="20sp"
            android:layout_gravity="center"
            android:text="Title" />
       <!-- Body goes here -->
        <ScrollView
            android:layout_weight="1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content">
            <TextView 
                android:id="@+id/text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:autoLink="web"
                android:text="@string/lorem_ipsum"
                android:textColor="#FFFFFF"

                android:padding="10dip" />
        </ScrollView>
        <!-- footer goes here -->
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content">
            <RelativeLayout
                android:layout_width="fill_parent"
                android:layout_height="fill_parent">
                <Button 
                    android:id="@+id/login_button"
                    android:layout_height="wrap_content"
                    android:layout_width="wrap_content"
                    android:layout_gravity="bottom"
                    android:layout_alignParentRight="true"
                    android:text="Button"/>

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接