【问题标题】:Getting Views to fit让视图适合
【发布时间】:2011-02-28 17:18:04
【问题描述】:

我有一个带有 LinearLayout 的 main.xml,其中包含三个项目:LinearLayout、ListView 和 LinearLayout。我希望 ListView 尽可能大,同时保持底部 LinearLayout 始终显示并且不会缩小或脱离屏幕。

我尝试将 ListView 的高度设为 fillparent,但是底部的 LinearLayout 没有显示出来,与 wrapcontent 一样,因为 ListView 有很多内容。

当我将 ListView 设置为设定大小时,比如 300dp,我在底部 LinearLayout 下方有空间。我尝试通过设置顶级 LinearLayout 重力=填充来解决此问题,但这并没有帮助。

另外,根据我尝试使用的 android,底部的 LinearLayout 会从屏幕上掉下来,或者缩小。 如果相关,顶层 LinearLayout 设置为 fillparent 的高度。 我的目标是保持顶部和底部的 LinearLayout 来包装它们的内容,而中间的 ListView 填充剩下的东西......有什么建议吗? 提前感谢您的努力!

【问题讨论】:

    标签: android android-layout android-listview android-linearlayout


    【解决方案1】:

    我相信你可以将android:layout_weight="1"添加到ListView,ListView的高度设置为fill_parent,两个LinearLayouts设置为wrap_content。无论如何,我通常更喜欢使用RelativeLayout。您可以指定页眉对齐到屏幕顶部,页脚对齐到底部,以及 ListView 填充中间的空间,如下所示:

    <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"
            >
            //...insert stuff here
        </LinearLayout>
    
        <LinearLayout
            android:id="@+id/footer"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            >
            //...insert stuff here
        </LinearLayout>
    
        <ListView
            android:id="@+id/listview"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_above="@id/footer"
            android:layout_below="@id/header"
            />
    </RelativeLayout>
    

    【讨论】:

    • 感谢两个回答都很完美!而且,很高兴学习新的视图
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-03-07
    • 1970-01-01
    • 2022-11-30
    • 1970-01-01
    • 2020-02-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多