【问题标题】:How to arrange two linear layouts inside a relative layout in android?如何在android中的相对布局中安排两个线性布局?
【发布时间】:2016-07-17 13:38:27
【问题描述】:

所附图片包含 3 个布局

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

两种线性布局的尺寸相同并且重叠

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

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

【问题讨论】:

  • 嵌套布局不利于性能。考虑在您的 RelativeLayout 中重叠视图。

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


【解决方案1】:

来自安卓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>

LinearLayouts 重叠的顺序与它们的定义顺序相对应。

【讨论】:

  • 不错的答案几乎我们的答案都相同@Svit,尝试解释事情是如何运作的,为你+1
【解决方案2】:

这是另一种方式

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

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

【讨论】:

  • 但它会在不同的屏幕上导致问题,想要的纵横比无法准确保持@Christlin
  • 我不这么认为,因为对于不同的屏幕密度,您将拥有不同的布局文件。因此,为每种类型的屏幕调整您的高度属性。然后就好了。
  • 但还有很长的路要走,不是更可取的@Christlin
  • 好的,没问题,你的就行了。我已经提到这是另一种方式:) @parmar
  • @ChristlinJoseph 试过了!但是两个线性布局仍然在顶部对齐。注意:其中一种线性布局是嵌套的。
【解决方案3】:

问得好,您可以使用 PercentRelativeLayout 而不是 RelativeLayout,这样您就可以调整 90% 的高度,然后使用此属性

android:layout_alignParentTop = true 

为您的第一个LinearLayout

android:layout_alignParentBottom = true 

第二个RelativeLayout

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

请参阅此问题了解如何使用 PercentRelativeLayout here

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>

【讨论】:

    猜你喜欢
    • 2012-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多