【发布时间】:2011-08-23 00:13:29
【问题描述】:
我正在尝试选择 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?
【问题讨论】:
-
您能否发布更完整的代码示例来复制屏幕截图和问题?我和我的朋友有一个想法,但我们懒得把你没有提供的缺失部分放在一起。
-
我看看能不能在接下来的几天里把东西放在一起。
-
好吧,我终于有机会举了一个例子……在这里找到它:dl.dropbox.com/u/19767586/layout.test.zip。我还包括了两个屏幕,一个是它的样子,一个是它应该看起来的样子。
标签: android android-layout listactivity sticky-footer android-relativelayout