4

我想做一个选择ListActivity,类似于用于向快速启动屏幕添加快捷方式的选项。我已经推出了自己的页眉和页脚,我希望在屏幕上在视图的顶部和底部“粘”。为了做到这一点,我使用了一个RelativeLayout,头部设置为顶部,页脚设置为底部,列表设置为低于页眉和页脚上方。就整个活动的布局而言,这是我所期望的渲染。标题粘到顶部,页脚粘到底部,列表滚动到它们之间。Android的RelativeLayout和wrap_content的高度?

当我切换到RelativeLayout作为我的根时发生了一件奇怪的事情。请看下面的截图:

enter image description here

我想我的活动的身高是wrap_content,这样的形式是只高在它显示的内容,但一旦我切换到RelativeLayout的,似乎呈现该活动有效地作为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?

+0

您能够发布更完整的代码示例复制截图和问题?我的朋友和我有一个想法,但我们懒得把你没有提供的遗失物品放在一起。 –

+0

我会在接下来的几天里看看我能不能把东西扔在一起。 – eidylon

+0

嗯,我终于有机会,并举了一个例子...在这里找到它:http://dl.dropbox.com/u/19767586/layout.test.zip。我还包括两个屏幕,一个是它的外观,一个是它应该看起来像。 – eidylon

回答

24

根据开发者文档所需的行为是不可能的相对布局;(

“需要注意的是,你不能有RelativeLayout的大小和它的孩子们的位置之间循环依赖。例如,你。不能有一个RelativeLayout的,其高度设为WRAP_CONTENT和儿童设置为ALIGN_PARENT_BOTTOM。“

RelativeLayout

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

你可以在屏幕高度,这里所描述get screen height

+0

Drat!哦,好吧......在此期间我已经重新编写了一个不同的布局,但这肯定会很好。 : - ? – eidylon

+1

我有完全相同的问题。最好的Android碎片。如果列表足够长,LinearLayout将旋转布局的页脚,使其在底部压缩到零。好难过。 – halxinate

+0

我想在底部视图之上放置另一个视图,但是我遇到了这个问题,所以我按照逆序排列:将顶部视图设置为'ALIGN_PARENT_TOP'(从问题转义)并将底部视图设置为'layout_below'。我的问题,然后修复 –

0

我只是改变我的RelativeLayout的的FrameLayout,所有开始工作

相关问题