Android RelativeLayout和wrap_content高度?

5
我正在尝试创建一个选择ListActivity,类似于用于将快捷方式添加到启动器屏幕的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完成的,我不会在代码中进行任何布局。如何在整个activity的高度以wrap_content模式运行的同时获得粘性标题和页脚?除了RelativeLayout之外,我应该采用其他方法吗?


你能否发布一个更完整的代码示例来复制屏幕截图和问题?我和我的朋友有一个想法,但我们懒得组合你没有提供的缺失部分。 - Thane Anthem
我会看看能否在接下来的几天里抽出时间做点什么。 - eidylon
好的,我终于有机会了,放了一个例子...在这里找到它:http://dl.dropbox.com/u/19767586/layout.test.zip。我还包括了两个屏幕截图,一个是它的实际样子,一个是它应该的样子。 - eidylon
2个回答

25
根据开发者文档,相对布局不支持您想要的行为;
注意,RelativeLayout 的大小和其子控件的位置之间不能存在循环依赖关系。例如,您不能将高度设置为 WRAP_CONTENT 并将子项设置为 ALIGN_PARENT_BOTTOM。
为解决问题,可能可以尝试使用线性布局,将其设置为 wrap_content,并通过代码将最大高度设置为屏幕高度。
您可以按照此处描述的方式获取屏幕高度。 RelativeLayout get screen height

糟糕!唉,没关系...与此同时,我已经用不同的布局重新编写了所有内容,但这肯定很好。 :-? - eidylon
1
我有完全相同的问题。这就是Android碎片化的表现。如果列表足够长,LinearLayout将使布局的页脚出现问题,使其在底部挤压到零。太悲哀了。 - halxinate
我想把另一个视图放在底部视图上面,但是我遇到了这个问题,所以我按相反的顺序操作:将顶部视图设置为'ALIGN_PARENT_TOP'(逃离问题),并使用'layout_below'将底部视图设置在其下方。然后我的问题得到了解决。 - Mohsen Afshin

1
我只是将我的RelativeLayout更改为FrameLayout,然后一切开始正常工作了。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接