FrameLayout wrap_content

4

我有一个关于FrameLayout的问题。我想创建一个FrameLayout,它的第一个子元素的高度和第二个子元素的父元素高度相同(在这个例子中是100dp)。但是我得到的结果是FrameLayout填满了整个屏幕(使用了match_parent)。 以下是我的xml代码。

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <View
        android:layout_width="match_parent"
        android:layout_height="100dp" />

    <View
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</FrameLayout>

我该如何使FrameLayout与其第一个子项一样大,而第二个子项与其父容器一样大?我需要使用不同的父容器吗?

使用LinearLayout代替FrameLayout。 - Padma Kumar
FrameLayout 旨在阻挡屏幕上的某个区域以显示单个项。通常,应使用 FrameLayout 来容纳单个子视图,因为如果不重叠彼此,则很难以可伸缩的方式组织子视图以适应不同的屏幕尺寸。 - Padma Kumar
我想将它们堆叠在一起,每次只显示一个视图。因此LinearLayout不可用。 - holtaf
1个回答

10

使用RelativeLayout肯定可以帮助您达到您想要实现的目标:

  <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <View
            android:id="@+id/view_id_1"
            android:layout_width="match_parent"
            android:layout_height="100dp" />

        <View
            android:id="@+id/view_id_2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/view_id_1"
            android:layout_alignTop="@+id/view_id_1" />
    </RelativeLayout>

希望这能帮到你!


谢谢,这就解决了问题)(没想到用RelativeLayout) - holtaf
我认为应该是: android:layout_alignBottom="@id/view_id_1" android:layout_alignTop="@id/view_id_1" 你仍然有加号。 - Brian F Leighty

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