5

我正在尝试选择 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吗?

4

2 回答 2

24

根据开发人员文档,相对布局无法实现您想要的行为;

“请注意,RelativeLayout 的大小与其子级的位置之间不能存在循环依赖关系。例如,不能有一个高度设置为 WRAP_CONTENT 且子级设置为 ALIGN_PARENT_BOTTOM 的 RelativeLayout。”

相对布局

为了解决您的问题,您可以尝试使用线性布局,设置为 wrap_content 并通过代码将最大高度设置为屏幕高度。

您可以按照此处所述获取屏幕高度:获取屏幕高度

于 2011-07-29T13:52:29.877 回答
1

我只是在FrameLayout中更改了我的RelativeLayout并开始工作

于 2016-11-28T17:36:29.437 回答