【问题标题】:Wrap linearlayouts in relativelayout将线性布局包装在相对布局中
【发布时间】:2016-11-29 11:42:37
【问题描述】:

我正在尝试将两个 LinearLayouts 放在一个 RelativeLayout(根元素)中。我只能看到一排。 ÄÄandroid:layout_below** 不应该强制第二个 Linearlayout 位于第一个 LinearLayout 之下吗?

怎么了?是不是因为LinearLayout本身就是一个ViewGroup?

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">

<LinearLayout
    android:id="@+id/addon1_row"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <ImageView
        android:id="@+id/weapon1_id"
        android:layout_width="100dp"
        android:layout_height="26dp"
        android:src="@mipmap/weapon1"
        android:adjustViewBounds="true"
        />

    <ImageView
        android:layout_width="100dp"
        android:layout_height="26dp"
        android:src="@mipmap/buy_button"
        android:adjustViewBounds="true"
        />
</LinearLayout>

<LinearLayout
    android:id="@+id/addon2_row"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:layout_below="@+id/addon1_row">

    <ImageView
        android:id="@+id/weapon2_id"
        android:layout_width="100dp"
        android:layout_height="26dp"
        android:src="@mipmap/weapon2"
        android:adjustViewBounds="true"
        />

    <ImageView
        android:layout_width="100dp"
        android:layout_height="26dp"
        android:src="@mipmap/buy_button"
        android:adjustViewBounds="true"

        />
</LinearLayout>


</RelativeLayout>

【问题讨论】:

  • 考虑到您当前的安排,您实际上并不需要 RelativeLayout。但我会通过使用没有 LinearLayouts 的单个 RelativeLayout 或使用 GridView(同样,没有额外的 LinearLayouts)来优化它(为了性能)。
  • @Rotwang,谢谢,我会考虑的

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


【解决方案1】:

这是因为你将两个线性布局的高度都设置为"match_parent",只需将其设置为"wrap_content"

<LinearLayout
    android:id="@+id/addon1_row"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"   <== Here
    android:orientation="horizontal">


<LinearLayout
    android:id="@+id/addon2_row"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"   <== Here
    android:layout_below="@+id/addon1_row"
    android:orientation="horizontal">

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-12-12
    • 1970-01-01
    • 1970-01-01
    • 2012-07-06
    • 2012-12-17
    • 1970-01-01
    • 2023-03-09
    相关资源
    最近更新 更多