android - 为什么 LinearLayout 不能正确显示嵌套的 FrameLayout?

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

我有一个 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>

</LinearLayout>

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

是否有 RelativeLayout 具有而 LinearLayout 没有的调整大小属性或权重分布?我一直在网上寻找,但不幸的是没有找到答案。

谢谢!

最佳答案

在您展示的代码中,第一个 FrameLayout 匹配父项的高度,因此后面的 LinearLayout 被推到屏幕外(y 轴,而不是 z 轴) FrameLayout。这是因为它们的父级是 LinearLayout,它将子级垂直(y 轴)排列在另一个下方,不重叠(就像在一卷卫生纸中,每个 View 都是一个纸正方形) .

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

至于为什么没有指定布局参数的 RelativeLayout 给了您预期的行为,这是因为默认情况下是将 subview 放置在容器的左上角(来自 RelativeLayout documentation ):

By default, all child views are drawn at the top-left of the layout, so you must define the position of each view using the various layout properties available from RelativeLayout.LayoutParams.

关于android - 为什么 LinearLayout 不能正确显示嵌套的 FrameLayout?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21470253/

相关文章:

android - Imagebutton 缓慢变为按下状态

安卓布局 : How to keep right-most text element and ellipsize left-most text element as it grows?

android - 如何获取工具栏的自定义 View

java - 与 Volley 建立联系

android - 在 Android 中设置背景颜色并使用 Drawable 作为背景

java - 我可以在 ViewFlipper 中设置 contentView 吗?

android - 将复选框对齐到LinearLayout android中的右侧

android - 在 OkHttp 中使用安全的 websockets (wss)

android 寻呼机滑动标签条 fragment 更换器

android - 如何使 ListView header 静态化