【问题标题】:How to put layout between others layout in RelativeLayout?如何在RelativeLayout中的其他布局之间放置布局?
【发布时间】:2017-12-31 02:26:26
【问题描述】:

我有一个RelativeLayout。在里面我有:

  • ImageView 120x120 dp 在右侧。
  • 左侧还有 3 个其他布局:
    • 第一个布局(称为Top)有alignParentTop=true
    • 第二个布局(称为底部)有alignParentBottom=true
    • 第三种布局(称为Middle)位于中间(Top下方,Bottom上方)。

问题是:如果我为容器(RelativeLayout)设置layout_width="wrap_content",我看不到Middle布局。

如果我将其设置为某些值(例如:144dp),我将看到 Middle 布局。

这是布局结构(我在里面隐藏了一些子布局,只显示主布局)。

 <?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="wrap_content"
    android:padding="16dp">

    <LinearLayout
        android:id="@+id/top"
        android:background="#eeaaee"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_toLeftOf="@+id/image"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    </LinearLayout>

    <LinearLayout
        android:id="@+id/bottom"
        android:background="#22eeaa"
        android:layout_toLeftOf="@+id/image"
        android:layout_alignParentStart="true"
        android:layout_alignParentBottom="true"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    </LinearLayout>

    <LinearLayout
        android:id="@+id/middle"
        android:layout_width="match_parent"
        android:background="#44ee22"
        android:layout_toLeftOf="@+id/image"
        android:layout_height="64dp"
        android:layout_below="@+id/top"
        android:layout_above="@+id/bottom">
        <TextView
            android:id="@+id/hotnews_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textStyle="bold"
            android:textSize="14sp"
            />
    </LinearLayout>

    <ImageView
            android:id="@+id/image"
            android:layout_alignParentEnd="true"
            android:layout_width="120dp"
            android:layout_height="120dp"
            android:scaleType="centerCrop"/>
</RelativeLayout>

【问题讨论】:

  • 也许我不明白你的问题,在你的中间布局中尝试 android:layout_alignParentLeft="true" 。希望对您有所帮助。
  • @AndreaEbano 它不起作用。
  • 您可以将高度设置为 match_parent
  • 我测试了你的代码,当我将 layout_width="wrap_content" 设置为容器时,我也看到了中间布局,你可以尝试清理和重建项目。

标签: android android-layout android-view android-relativelayout view-hierarchy


【解决方案1】:

如果你想在某个布局之间放置布局,最好使用线性布局作为所有三个(顶部、中间、底部)布局的父级。并使用它的方向和重量。我使用线性布局的固定高度来区分顶部、中间和底部视图。根据你的需要改变它。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="16dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:orientation="vertical">

            <LinearLayout
                android:id="@+id/top"
                android:layout_width="match_parent"
                android:layout_height="40dp"
                android:background="#eeaaee"
                android:orientation="horizontal"></LinearLayout>

            <LinearLayout
                android:id="@+id/bottom"
                android:layout_width="match_parent"
                android:layout_height="40dp"
                android:background="#22eeaa"
                android:orientation="horizontal" />

            <LinearLayout
                android:id="@+id/middle"
                android:layout_width="match_parent"
                android:layout_height="40dp"
                android:background="#44ee22">

                <TextView
                    android:id="@+id/hotnews_title"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:textSize="14sp"
                    android:textStyle="bold" />
            </LinearLayout>


        </LinearLayout>

        <ImageView
            android:id="@+id/image"
            android:layout_width="120dp"
            android:layout_height="120dp"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:layout_weight="1"
            android:scaleType="centerCrop" />


    </LinearLayout>


</RelativeLayout>

【讨论】: