为什么LinearLayout没有正确显示嵌套的FrameLayout?

时间:2014-01-31 00:13:55

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

我有一个XML布局,我在LinearLayout中嵌套了FrameLayout,并尝试在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没有?我一直在网上看,但遗憾的是没有找到答案。

谢谢!

2 个答案:

答案 0 :(得分:4)

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

如果您希望孩子FrameLayoutLinearLayout位于另一个(z轴)的顶部,则他们的父母必须是FrameLayout,这会将其安排在其中另一个在z轴上重叠(如在一叠纸张中)。

至于为什么没有指定布局参数的RelativeLayout给出了您预期的行为,这是因为默认情况下将子视图放在容器的左上角角落(来自{{ 3}}):

  

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

答案 1 :(得分:0)

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

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

相关问题