【问题标题】:Android RelativeLayout and height of wrap_content?Android RelativeLayout 和 wrap_content 的高度?
【发布时间】: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


【解决方案1】:

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

"请注意,大小之间不能有循环依赖关系 RelativeLayout 及其子项的位置。例如,你 不能有一个高度设置为 WRAP_CONTENT 的 RelativeLayout 和一个 子设置为 ALIGN_PARENT_BOTTOM。”

RelativeLayout

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

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

【讨论】:

  • 该死!哦,好吧......与此同时,我已经用不同的布局重写了,但这肯定会很好。 :-?
  • 我也有同样的问题。 Android 碎片化处于最佳状态。如果列表足够长,LinearLayout 将拧紧布局的页脚,使其在底部压缩为零。好伤心。
  • 我想在底部视图上方放置另一个视图,但是我遇到了这个问题,所以我按照相反的顺序进行了操作:将顶部视图设置为 'ALIGN_PARENT_TOP'(逃避问题)并设置底部使用“layout_below”查看。然后我的问题就解决了
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 2014-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多