Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the purpose of Android's <merge> tag in XML layouts?

I've read Romain Guy's post on the <merge /> tag, but I still don't understand how it's useful. Is it a sort-of replacement of the <Frame /> tag, or is it used like so:

<merge xmlns:android="...."> <LinearLayout ...>     .     .     . </LinearLayout> </merge> 

then <include /> the code in another file?

like image 567
cesar Avatar asked Jan 12 '12 12:01

cesar


People also ask

What is use of merge in Android XML?

Merge Tag. The <merge /> tag helps us to eliminate redundant view groups in our view hierarchy when including one layout within another.

What is the use of Merge tag in Android?

Use the <merge> tag The <merge /> tag helps eliminate redundant view groups in your view hierarchy when including one layout within another.

What is the purpose of androids layout?

Android Layout is used to define the user interface that holds the UI controls or widgets that will appear on the screen of an android application or activity screen. Generally, every application is a combination of View and ViewGroup.

What is layout tag Android?

The <layout> tag must be the root tag when you are using DataBinding . Doing so you are telling the compiler that you are using DataBinding and your layout will have special tags like <variable> or <import> , so you have to embed your layout within that tag.


1 Answers

<merge/> is useful because it can get rid of unneeded ViewGroups, i.e. layouts that are simply used to wrap other views and serve no purpose themselves.

For example, if you were to <include/> a layout from another file without using merge, the two files might look something like this:

layout1.xml:

<FrameLayout>    <include layout="@layout/layout2"/> </FrameLayout> 

layout2.xml:

<FrameLayout>    <TextView />    <TextView /> </FrameLayout> 

which is functionally equivalent to this single layout:

<FrameLayout>    <FrameLayout>       <TextView />       <TextView />    </FrameLayout> </FrameLayout> 

That FrameLayout in layout2.xml may not be useful. <merge/> helps get rid of it. Here's what it looks like using merge (layout1.xml doesn't change):

layout2.xml:

<merge>    <TextView />    <TextView /> </merge> 

This is functionally equivalent to this layout:

<FrameLayout>    <TextView />    <TextView /> </FrameLayout> 

but since you are using <include/> you can reuse the layout elsewhere. It doesn't have to be used to replace only FrameLayouts - you can use it to replace any layout that isn't adding something useful to the way your view looks/behaves.

like image 128
blazeroni Avatar answered Oct 22 '22 04:10

blazeroni