我正在尝试创建一个选择ListActivity,类似于用于将快捷方式添加到启动器屏幕的ListActivity。我已经自己编写了标题和页脚,并希望它们在屏幕上时“粘性”保持在视图的顶部和底部。为此,我使用RelativeLayout,其中标题设置为停靠在顶部,页脚设置为停靠在底部,列表设置为在标题下方和页脚上方。就活动的总体布局而言,这是我所期望的呈现方式。标题固定在顶部,页脚固定在底部,列表在它们之间滚动。
但是当我将RelativeLayout作为根时,出现了一个奇怪的问题。请参见以下截图:
我希望我的Activity的高度为wrap_content,以便表单的高度仅为其中显示的内容高度,但是一旦我切换到RelativeLayout,它似乎将Activity有效地呈现为fill_parent,占据整个屏幕,即使内容不需要。请注意,没有足够的列表项来填充屏幕,这样使用fill_parent样式会在列表结束和页脚之间留下大量的空白。我之前是通过样式来设置高度-这在LinearLayout中效果很好,但现在似乎被忽略了。因此,我尝试在RelativeLayout上直接硬编码高度,但它仍然不起作用,并且仍以fill_parent呈现。
以下是我的布局代码:
但是当我将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完成的,我不会在代码中进行任何布局。如何在整个activity的高度以wrap_content
模式运行的同时获得粘性标题和页脚?除了RelativeLayout之外,我应该采用其他方法吗?