Android 滚动视图和线性布局有困难

Android 滚动视图和线性布局有困难,android,scrollview,android-linearlayout,Android,Scrollview,Android Linearlayout,我正在尝试做一个Android布局:3个组件在一个垂直的线性布局中。中心组件是一个滚动视图,其中包含一个文本视图。当文本视图包含大量文本(超出屏幕的容纳范围)时,滚动视图会一直延伸到屏幕底部,显示滚动条,并将最后一个组件,即带有按钮的线性布局推离屏幕。 如果滚动视图中的文本视图中的文本足够短,则屏幕底部的按钮位置正确。 我试图实现的布局是: 我编写的布局的XML是: <?xml version="1.0" encoding="UTF-8"?> <LinearLayo

我正在尝试做一个Android布局:3个组件在一个垂直的线性布局中。中心组件是一个
滚动视图
,其中包含一个
文本视图
。当
文本视图
包含大量文本(超出屏幕的容纳范围)时,
滚动视图
会一直延伸到屏幕底部,显示滚动条,并将最后一个组件,即带有
按钮的
线性布局
推离屏幕。

如果
滚动视图
中的
文本视图
中的文本足够短,则屏幕底部的按钮位置正确。

我试图实现的布局是:

我编写的布局的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>


scrollview是第二个视图对象,设置为包装内容,这比屏幕上的内容要多

我推荐一个相亲。顶部文本视图首先使用
android:alignParentTop=“true”
,底部线性布局接下来是
android:alignParentBottom=“true”
,滚动视图在xml中最后列出,值为
android:aligndown=“@id/whatYouCallTheHeader


这将使屏幕底部的底部栏与顶部的页眉对齐,无论大小如何。这样,在放置页眉和页脚后,滚动视图将有自己的位置。

您应该选择relativeLayout,而不是LinearLayout。您可以使用一些属性,如AlignDown和all。

尝试添加布局在
滚动视图中添加权重
ie

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

这在一种与您介绍的情况几乎相同的情况下对我有效,但让我想知道为什么,因为将控件的布局权重从0(如果不指定
layout\u weight
)增加到1会使已经占用过多空间的控件变小,这是违反直觉的


我怀疑它起作用的原因是,通过不指定布局权重,实际上允许布局忽略相对于其他控件的滚动视图的大小,反之,如果指定了一个,则允许布局根据指定的权重按比例缩小。

![固定页眉页脚和可滚动正文布局][1]


这就是你想要的。安卓系统中的大多数应用都有这种布局, 一个固定的页眉和页脚以及一个可滚动的正文



如果您将1.6作为目标编写,它只会在xml中传递一次,因此任何引用都必须在被引用之前进行布局。2.1+会传递两次。
<?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>