Simple example of <merge> and <include> usage in Android XML-layouts

I'm curious about the <merge> and <include> tags in Android XML-layouts. I've read two tutorials, but haven't yet found a simple example usage.

Would be happy if someone could provide such an example or give a pointer to one.

57730 次浏览

some_activity.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:orientation="vertical">


// some views


<include layout="@layout/view_part"/>


// probably more views


</LinearLayout>

view_part.xml:

<merge xmlns:android="http://schemas.android.com/apk/res/android">


// the views to be merged


</merge>

id doesn't paste code otherwise relative layout parameters would have worked. It does some different processing

There's a simple Android XML layout <include /> HOWTO that's also explaining a common pitfall over at http://www.coboltforge.com/2012/05/tech-stuff-layout/. That may help...

Take an example:

I have two tags <EditText> and <ListView > coming more than one UIs. So I created an XML file as given below to include in all such UI's.

<?xml ...>
<EditText ... />
<ListView ... />

The above XML is not valid XML since it did not have a root element. So a root element is needed just for the sake of XML. <merge> is the solution as given below:

<?xml ...>
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<EditText ... />
<ListView ... />
</merge>

<merge> tag is used to mitigate the number of the levels to increase the performance of rendering layouts. tag is used with <include> tag perfectly together.

Take an example, we have a login layout and used for more than one in scope of our app. While using tag to show login_layout, we can use and can escape a level.

I also advise you to read the tricks about layouts. http://android-developers.blogspot.com.tr/2009/03/android-layout-tricks-3-optimize-by.html

login_form.xml

<?xml version="1.0" encoding="utf-8"?>
<!-- Login form -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<EditText
android:id="@+id/email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Email..."
android:inputType="textEmailAddress"
android:maxLines="1"
android:singleLine="true"
android:visibility="visible" />


<EditText
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password.."
android:imeActionId="@+id/login"
android:imeOptions="actionUnspecified"
android:inputType="textPassword"
android:maxLines="1"
android:singleLine="true"
android:text="1337"
android:visibility="visible" />


<Button
android:id="@+id/sign_in_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="16sp"
android:paddingLeft="32sp"
android:paddingRight="32sp"
android:text="Login"
android:visibility="visible" />


</LinearLayout>

example_layout.xml (any layout we want to include login_form.xml)

<merge xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" >


<include layout="@layout/login_form" />


</merge>

We can see the level hierarchy enter image description here