ScrollView和LinearLayout难度大

时间:2011-09-04 05:34:19

标签: android scrollview android-linearlayout

我正在尝试制作Android布局:垂直LinearLayout中的3个组件。中心组件是ScrollView,其中包含TextView。当TextView包含大量文本(超出屏幕尺寸)时,ScrollView会一直增长到屏幕底部,显示滚动条,然后推送最后一个组件,屏幕外有LinearLayout的{​​{1}}。

如果Button内的TextView内的文字足够短,则屏幕底部的按钮位置完美。

我想要实现的布局是:

我写的布局的XML是:

ScrollView

4 个答案:

答案 0 :(得分:6)

scrollview是第二个视图对象,设置为wrap_content,它不仅仅是屏幕。

我推荐一个RelativeLayout。顶部textview首先使用android:alignParentTop="true",最下面的LinearLayout使用android:alignParentBottom="true",最后在xml中列出的滚动视图的值为android:alignBelow="@id/whatYouCallTheHeader

无论大小如何,这都将对齐屏幕底部的底栏和顶部的标题。然后,在页眉和页脚放置后,scrollview将有自己的位置。

答案 1 :(得分:2)

你应该去relativeLayout而不是LinearLayout。你可以使用一些属性,比如alignBelow和all。

答案 2 :(得分:2)

尝试将布局权重添加到ScrollView

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

这对我来说在一个与你呈现的情况几乎完全相同的情况下工作但让我想知道为什么,因为从0开始增加控件的布局权重是违反直觉的(如果你没有指定则默认一个layout_weight)到1应该使一个已经占用太多空间的控件。

我怀疑它的工作原理是,通过不指定layout_weight,你实际上允许布局忽略滚动视图相对于其他控件的大小,相反,如果指定一个,你给它允许缩小它的比例与你指定的权重。

答案 3 :(得分:2)

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


这就是你要找的东西。 android中的大多数应用程序都有这种类型的布局, 固定的页眉和页脚以及可滚动的主体。此布局的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>