Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why adding <merge> changes the android layout?

I try to include xml_layoutA into xml_layoutB.

At first I didn't know i have to add <merge> tag in xml_layoutA.

but then I added this tag and then the xml_layoutA started being aligned to left instead of to the right as before.

What has caused this change?

xml_layoutA.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tooltip_layout"
    android:layout_width="262dp"
    android:layout_height="92dp"
    android:background="@drawable/tip_tool_top_right"
    android:orientation="horizontal"
    android:paddingTop="10dp"
    android:paddingBottom="15dp"
    android:paddingLeft="5dp"
    android:paddingRight="10dp" >

    <LinearLayout
   ...
    </LinearLayout>

</LinearLayout> 

vs.

<merge xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
    android:id="@+id/tooltip_layout"
    android:layout_width="262dp"
    android:layout_height="92dp"
    android:background="@drawable/tip_tool_top_right"
    android:orientation="horizontal"
    android:paddingTop="10dp"
    android:paddingBottom="15dp"
    android:paddingLeft="5dp"
    android:paddingRight="10dp" >

    <LinearLayout
...

    </LinearLayout>
</LinearLayout>
</merge>

both case in same xml_layoutB.xml

in RelativeLayout:

    <include
        android:id="@+id/tooltipFriends"  
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@id/friendsonline_bar"
        android:layout_alignParentRight="true"
        android:visibility="visible" 
        layout="@layout/tooltip_friends"  />    
like image 691
Elad Benda Avatar asked Nov 11 '22 11:11

Elad Benda


1 Answers

The problem you're facing is caused because those include's parameters are ignored when using the merge tag.

Anyway the merge tag you have there is not needed and it's not doing much for you there. Basically the merge tag is useful when you have more view and you want those views to be included in unknown layout. For example, you have those 4 tags you want to be reused

<merge>
  <TextView ... />
  <TextView ... />
  <EditText ... />
  <Button ... />
</merge>

Then when you use this:

<RelativeLayout>
  <include layout="^^" />
</RelativeLayout>

What you'll get is this:

<RelativeLayout>
  <TextView ... />
  <TextView ... />
  <EditText ... />
  <Button ... />
</RelativeLayout>

Any parameters on the original include tag will be discarded, because there is no way to tell to which tag they should be added.

like image 54
Tadeas Kriz Avatar answered Nov 14 '22 21:11

Tadeas Kriz