<Merge/>:
The <merge/> label is introduced separately because it plays an important role in optimizing the UI structure. The objective is to optimize the entire android layout structure by deleting redundant or additional layers.
We will use an example to understand the actual functions of this tag, so that we can more intuitively understand the usage of <merge/>.
Create a simple layout with two views elements: imageview and textview. By default, these two elements are placed in framelayout. The effect is to display an image in full screen in the main view, and then display the title on the image, which is located below the view. Below is XMLCode:
<Framelayout xmlns: Android =" Http://schemas.android.com/apk/res/android "Android: layout_width =" fill_parent "Android: layout_height =" fill_parent "> <imageview Android: layout_width =" fill_parent "Android: layout_height =" fill_parent "Android: scaletype = "center" Android: src = "@ drawable/golden_gate"/> <textview Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: layout_marginbottom = "20dip" Android: layout_gravity = "center_horizontal | bottom" Android: padding = "12dip" Android: Background = "# aa000000" Android: textcolor = "# ffffffff" Android: TEXT = "Golden Gate"/> </framelayout>
The layout running view on the application is as follows:
Start tools> hierarchyviewer. BAT to view the current UI Structure View:
We can see that two framelayout nodes appear in the structure contained by the red box, obviously, these two nodes with the same meaning cause a waste of resources (here we can remind you that you can habitually view the current UI resource allocation through hierarchyviewer in the Development Project ), so how can we solve this problem (in the current example, how can we remove redundant framelayout nodes )? In this case, the <merge/> label is used to solve similar problems. We replace framlayout in the above XML code with merge:
<! -Easy adsenser v2.37-> <! -Post [count: 2]->
RunProgramThe effect shown in emulator is the same, but the UI structure viewed through hierarchyviewer has changed. The redundant framelayout nodes were merged, you can also add a subset of the merge label to the framelayout and node of the activity directly. (Note that the root nodes of all activity views are framelayout ). If the created layout does not use framlayout as the root node (but uses linerlayout to define root labels), you cannot use the preceding example to optimize the UI structure through merge.
Besides the preceding example, meger has another usage.
When the include or viewstub label is used to import the XML structure from outside, the imported XML can be represented by merge as the root node, in this way, after being embedded into the parent structure, the subset it contains can be well integrated into the parent structure without redundant nodes.
Pay special attention to the following two points: