我正在尝试选择 ListActivity,类似于用于向启动器屏幕添加快捷方式的选择。我已经滚动了自己的页眉和页脚,我希望在屏幕上时在视图的顶部和底部“粘”。为了做到这一点,我使用了一个RelativeLayout,其中页眉设置为停靠在顶部,页脚设置为停靠在底部,列表设置为位于页眉下方和页脚上方。就活动的整体布局而言,这是我所期望的渲染。页眉粘在顶部,页脚粘在底部,列表在它们之间滚动。
当我切换到 RelativeLayout 作为我的根时,发生了一件奇怪的事情。请看下面的截图:
我希望我的 Activity 的高度为wrap_content
,以便表单仅与其中显示的内容一样高,但是一旦我切换到 RelativeLayout,它似乎可以有效地渲染 Activity fill_parent
,占用整个屏幕,即使内容没有不保证。请注意,没有足够的列表项来填满屏幕,这与fill_parent
样式一起在列表末尾和页脚之间留下了一堆空白。我正在通过样式设置我的高度 - 这与 LinearLayout 配合得很好,但现在似乎被忽略了。所以我尝试直接在 RelativeLayout 上对高度进行硬编码,但它仍然不起作用并且仍然呈现为fill_parent
.
这是我的布局代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
style="@style/GroupsList"
android:layout_height="wrap_content">
<FrameLayout android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:id="@+id/hdrGroups"
android:layout_alignParentTop="true">
<include layout="@layout/title"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</include>
</FrameLayout>
<FrameLayout style="@style/MyFooter"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_alignParentBottom="true"
android:id="@+id/ftrGroups">
<ImageView style="@style/CloseButton"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:src="@drawable/add"
android:id="@+id/imgGroupsAdd"
android:clickable="true">
</ImageView>
</FrameLayout>
<ListView android:divider="#9f9f9f"
android:id="@+id/android:list"
android:choiceMode="singleChoice"
android:dividerHeight="1dp"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_below="@id/hdrGroups"
android:layout_above="@id/ftrGroups">
</ListView>
<TextView android:text="@string/browser_no_groups"
style="@style/ListedItemB"
android:id="@+id/android:empty"
android:layout_height="wrap_content"
android:layout_above="@id/ftrGroups"
android:layout_below="@id/hdrGroups"
android:layout_width="fill_parent">
</TextView>
</RelativeLayout>
所有布局都是通过 XML 完成的,......我没有在代码中做任何布局。如何获得粘性页眉和页脚,同时让整个活动在wrap_content
其高度模式下运行?还有其他方法我应该解决这个问题而不是RelativeLayout吗?