Search code examples
androidandroid-layout

Android - Positioning elements inside <merge /> tag



I've just learned about the <merge /> tag and I'm trying to position the elements inside it as I was in a RelativeLayout container.
Nothing, except layout_gravity, seems to be working.
What am I doing wrong ?
Thx anticipately


Solution

  • There is a workaround by using tools namespace.

    You need to create a second layout file like this:

    <!-- linear.xml -->
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">
        <include layout="@layout/merged"/>
    </LinearLayout>
    

    Then you can go ahead and use the showIn attribute:

    <?xml version="1.0" encoding="utf-8"?>
    <merge
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        tools:showIn="@layout/linear"> <!-- HERE the fun stuff -->
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Test"/>
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Test"/>
    </merge>
    

    Doing so, the items will be displayed like if it was a LinearLayout and the same applies to every other layout type.