How to put layout between others layout in RelativeLayout?

280 Views Asked by At

I have a RelativeLayout. Inside it I have:

  • An ImageView 120x120 dp in the right.
  • 3 other layouts in the left:
    • 1st layout (called Top) has alignParentTop=true
    • 2nd layout (called Bottom) has alignParentBottom=true
    • 3rd layout (called Middle) is at the middle (below Top and above Bottom).

The problem is: if I set layout_width="wrap_content" for the container (RelativeLayout), I don't see the Middle layout.

And if I set it to some values (for example: 144dp) I will see the Middle layout.

Here is layout structure (I hide some child layouts inside it and shows only main layouts).

 <?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>
1

There are 1 best solutions below

0
On

if you want to put layout between some layout than its better to use linearlayout as parent of all the three (top,middle,bottom) layout.and use its orientation and weight. i have used fixed height of linearlayout just to differenciate the top,middle and bottom view. change it according to your need.

<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>