Android memory optimization is a very important issue, and UI optimization is a top priority.
This label plays an important role in optimizing the UI structure. It aims to optimize the entire UI Layout structure by deleting redundant or additional layers. 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. The following is the xml code:
Xml Code
<FrameLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent">
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: scaleType = "center"
Android: src = "@ drawable/golden_gate"/>
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>
Start tools> hierarchyviewer. bat to view the current UI Structure View:
We can see that there are two framelayout nodes in the structure contained by the red box. Obviously, the two nodes with the same meaning cause a waste of resources, in this case, you can use the merge label to solve the problem. Replace the root node with the merge label as follows:
Xml Code
<Merge xmlns: android = "http://schemas.android.com/apk/res/android">
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: scaleType = "center"
Android: src = "@ drawable/golden_gate"/>
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"/>
After running the program, the Emulator displays the same effect. However, the UI structure viewed through hierarchyviewer has changed. The redundant FrameLayout nodes were merged, you can also add a subset of the merge tag to the FrameLayout root node of the Activity. (Note: 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.
In addition to the preceding example, meger also has another usage: When the Include or ViewStub label is used to import the xml structure from outside, merge can be used as the root node for the imported xml, 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:
A. It can only be used as the root node of xml layout;
B. If the xml layout to be expanded is based on merge as the root node, You need to place the imported xml layout in the viewGroup, and set attachToRoot to True.