Android RelativeLayout和wrap_content的高度?

eid*_*lon 4 android listactivity sticky-footer android-layout android-relativelayout

我正在尝试选择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>
Run Code Online (Sandbox Code Playgroud)

所有布局都是通过XML完成的,......我没有在代码中做任何布局.我如何获得粘性页眉和页脚,同时让整个活动在wrap_content其高度模式下运行?还有其他方法我应该去做这个而不是RelativeLayout吗?

Die*_*ner 24

根据开发人员文档,相对布局无法实现所需的行为;(

"请注意,您不能在RelativeLayout的大小与其子项的位置之间存在循环依赖关系.例如,您不能将RelativeLayout的高度设置为WRAP_CONTENT且子项设置为ALIGN_PARENT_BOTTOM."

的RelativeLayout

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

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