Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 507
Andrea Tulimiero Avatar asked Oct 22 '25 22:10

Andrea Tulimiero


2 Answers

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.

like image 185
David Medenjak Avatar answered Oct 24 '25 20:10

David Medenjak


Use tools:parentTag on <merge> element.

<merge
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:parentTag="androidx.constraintlayout.widget.ConstraintLayout">

    <Button ...layout as you would in ConstraintLayout />

</merge>
like image 41
sidon Avatar answered Oct 24 '25 18:10

sidon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!