gpt4 book ai didi

Android RelativeLayout 和 wrap_content 的高度?

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:33:30 29 4
gpt4 key购买 nike

我正在尝试选择 ListActivity,类似于用于向启动器屏幕添加快捷方式的选择。我已经滚动了自己的页眉和页脚,我希望它们在屏幕上时“粘”在 View 的顶部和底部。为此,我使用了一个 RelativeLayout,其中页眉设置为停靠在顶部,页脚设置为停靠在底部,列表设置为位于页眉下方和页脚上方。就 Activity 的整体布局而言,这是我所期望的呈现方式。页眉粘在顶部,页脚粘在底部,列表在它们之间滚动。

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

enter image description here

我希望我的 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 之外,还有其他方法可以解决这个问题吗?

最佳答案

根据开发者文档,您想要的行为对于相对布局是不可能的;

"Note that you cannot have a circular dependency between the size of the RelativeLayout and the position of its children. For example, you cannot have a RelativeLayout whose height is set to WRAP_CONTENT and a child set to ALIGN_PARENT_BOTTOM."

RelativeLayout

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

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

关于Android RelativeLayout 和 wrap_content 的高度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5959608/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com