【问题标题】:Why does LinearLayout not correctly display nested FrameLayout?为什么 LinearLayout 不能正确显示嵌套的 FrameLayout?
【发布时间】:2014-01-31 00:13:55
【问题描述】:

我有一个 XML 布局,其中我将一个 FrameLayout 嵌套在一个 LinearLayout 中,并尝试在 FrameLayout 顶部堆叠一个同级查看组。它根本不显示同级查看组,而只是显示 FrameLayout。我想请一些帮助来理解为什么将根 ViewGroup 替换为 RelativeLayout 会产生预期的输出。

这是布局:

<?xml version="1.0" encoding="utf-8"?>  
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:orientation="vertical">
     <FrameLayout  
         android:layout_width="match_parent"  
         android:layout_height="match_parent"
         android:id="@+id/mainFrameLayout"
         android:background="#787878"/>
    <LinearLayout
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:orientation="vertical">  
         <ImageView  
             android:id="@+id/ImageView01"  
             android:layout_height="wrap_content"  
             android:layout_width="match_parent"  
             android:src="@drawable/download"  
             android:adjustViewBounds="true"/>
         <TextView  
             android:layout_width="match_parent"  
             android:layout_height="wrap_content"  
             android:textColor="#000"  
             android:textSize="40sp"  
             android:text="Top text" />
         <TextView
             android:layout_width="match_parent"
             android:layout_height="wrap_content"
             android:layout_gravity="bottom"
             android:gravity="right"
             android:text="bottom_text"
             android:textColor="#fff"
             android:textSize="50sp" />
</LinearLayout>

&lt;/LinearLayout&gt;

我只看到 FrameLayout 本身填满了屏幕。当我将根 ViewGroup 更改为使用 layout_height 定义的 RelativeLayout 并将 layout_width 更改为“match_parent”时,我得到了预期的输出,其中 ImageView 和两个 TextView 都显示在 FrameLayout 的顶部。我对它是如何工作的感到困惑,因为我没有为子 ViewGroups/View 指定任何 RelativeLayout 参数,例如“bottomof”或“topof”。

RelativeLayout 有没有 LinearLayout 没有的调整大小属性或权重分布吗?我一直在网上找,可惜没有找到答案。

谢谢!

【问题讨论】:

  • 如果你仔细观察,你没有在框架布局标签中添加任何东西

标签: android android-layout android-linearlayout android-relativelayout android-framelayout


【解决方案1】:

在您显示的代码中,第一个 FrameLayout 与父级的高度匹配,因此随后的 LinearLayout 被推到屏幕下方(y 轴,而不是 z 轴)FrameLayout。这是因为它们的父级是 LinearLayout,它垂直(y 轴)将其子级排列在另一个下方,不重叠(如在一卷卫生纸中,每个视图都是一个正方形纸)。

如果您希望子 FrameLayoutLinearLayout 位于另一个(z 轴)之上,则它们的父级必须是 FrameLayout,这会将其子级安排在另一个之上重叠z 轴(就像一叠纸一样)。

至于为什么 RelativeLayout 没有指定布局参数会给你预期的行为,这是因为默认是将子视图放置在容器的左上角角(来自RelativeLayout documentation) :

默认情况下,所有子视图都绘制在布局的左上角,因此您必须使用 RelativeLayout.LayoutParams 提供的各种布局属性来定义每个视图的位置。

【讨论】:

  • 完美的解释,感谢frozenkoi的帮助!
  • @frozenkoi +1 用于阐明不同布局之间的差异,并进一步用于描述 RelativeLayout 的默认行为。谢谢!
【解决方案2】:

您应该从 FrameLayout 中删除 match_parent,因为 Linearlayout 在垂直方向上一个接一个地放置视图。

在您的情况下,FrameLayout 负责占据整个设备屏幕,因此剩余的视图被推到屏幕外。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-06
    • 2015-10-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多