Search code examples
android-layoutandroid-linearlayoutandroid-xmlandroid-relativelayout

Wrap linearlayouts in relativelayout


I am trying to put two LinearLayouts in a RelativeLayout (rootelement). I can only see one row. Shouldn't the ÄÄandroid:layout_below** force the second Linearlayout to be positioned below the first LinearLayout?

What is wrong? Is it because LinearLayout in itself is a 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>

Solution

  • It's because of you have set the height of both linear layout to "match_parent" just set it to "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">