android 如何在相对布局内排列两个线性布局?

How to arrange two linear layouts inside a relative layout in android?

所附图片包含 3 个布局

  1. 相对布局
  2. 线性布局
  3. 线性布局

两个线性布局大小相同并且重叠

我只想知道如何在相对布局中安排这两个线性布局,以便线性 布局 1 & 线性布局 2 将有父身高的 90%。此外,线性布局 1 必须与相对布局的 top 对齐,而 线性布局 2相对布局的底部

任何简单的工作解决方案将不胜感激。(我是新手到android工作室)

好问题,你可以做的是使用 PercentRelativeLayout 而不是 RelativeLayout 这样你就可以调整 90% 的高度,然后使用这个 属性

android:layout_alignParentTop = true 

你的第一个 LinearLayout

android:layout_alignParentBottom = true 

第二个 RelativeLayout

它会根据需要将您的 LinearLayouts 粘在 RelativeLayout 中。

查看此问题了解如何使用 PercentRelativeLayout

Sample Layout here

如果你还想要分层效果,就像你在其中一张图片中看到的那样

android:elevation  property on your both `LinearLayout`.

代码取自 @Svit 的另一个答案以进行完整解释

<android.support.percent.PercentRelativeLayout
         xmlns:android="http://schemas.android.com/apk/res/android"
         xmlns:app="http://schemas.android.com/apk/res-auto"
         android:layout_width="match_parent"
         android:layout_height="match_parent">
     <LinearLayout
         android:layout_width="match_parent"
         app:layout_heightPercent="90%"
         android:layout_alignParentTop="true"/>
     <LinearLayout
         android:layout_width="match_parent"
         app:layout_heightPercent="90%"
         android:layout_alignParentBottom="true"/>
 </android.support.percent.PercentRelativeLayout>

这是另一种方式

  1. 将线性布局高度设置为您想要的水平(固定)
  2. 为两个线性布局(第一个布局顶部和第二个布局底部)指定 android:gravity 属性。

然后就可以得到想要的结果了

来自Android documentation:

<android.support.percent.PercentRelativeLayout
         xmlns:android="http://schemas.android.com/apk/res/android"
         xmlns:app="http://schemas.android.com/apk/res-auto"
         android:layout_width="match_parent"
         android:layout_height="match_parent">
     <LinearLayout
         android:layout_width="match_parent"
         app:layout_heightPercent="90%"
         android:layout_alignParentTop="true"/>
     <LinearLayout
         android:layout_width="match_parent"
         app:layout_heightPercent="90%"
         android:layout_alignParentBottom="true"/>
 </android.support.percent.PercentRelativeLayout>

LinearLayout 重叠的顺序与其定义的顺序一致。