1. Reusing layout code with <include/> Tags
If you need the same layout design in one project, you can reuse the layout code with the <include/> Tag, which is not covered in the Android development documentation. This tag is used in the Android Home screen program:
View plain 10 20 30 40 50 60 70 80 90 100 110 120 130 140 150
- <Com.android.launcher.Workspace
- android:id="@+id/workspace"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- launcher:defaultscreen="1">
- <include android:id= "@+id/cell1" layout="@layout/workspace_screen" />
- <include android:id= "@+id/cell2" layout="@layout/workspace_screen" />
- <include android:id= "@+id/cell3"layout= "@layout/workspace_screen" />
- </com.android.launcher.Workspace>
This allows you to refer to a layout fragment multiple times without duplicating, pasting. The include tag also allows you to override the values of some properties, such as the example above, which overwrite the ID value in the referenced layout. Here's another example:
View Plain
- <include android:layout_width="fill_parent" layout="@layout/image_holder"/>
- <include android:layout_width="256dip" layout="@layout/image_holder"/>
2. Use <merge/> Tags to reduce the view hierarchy in the Android layout file, you need a top-level container to hold the other components, rather than placing multiple components directly, such as the following code:
View Plain
- <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:text="Golden Gate"/>
- </FrameLayout>
The <merge/> Label is introduced separately because it plays an important role in optimizing the UI structure. The goal is to optimize the structure of the entire Android layout by cutting redundant or extra layers.
3. An example will be used to understand the actual effect of this tag, so that you can more intuitively understand the use of <merge/>.
Create a simple layout that contains two views elements: ImageView and TextView By default we place these two elements in Framelayout. The effect is to display a picture in full screen in the main view, then display the title on the picture and below it. The following is the XML code:
View plain 10 20 30 40 50 60 70 80 90 100 110 120 130 140 150
- <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 view used to run the above layout is:
Start the tools> Hierarchyviewer.bat tool to view the current UI structure view:
We can clearly see that the structure contained by the Red wireframe has two framelayout nodes, It is obvious that these two identically identical nodes cause a waste of resources (this can be a reminder of the habitual use of hierarchyviewer to view the allocation of current UI resources in development projects). So how do you solve this problem (how do you get rid of redundant framelayout nodes in the current example)? This is the time to use the <merge/> tag to deal with similar problems. We replace the framlayout in the above XML code with the merge:
View plain 10 20 30 40 50 60 70 80 90 100 110 120 130 140 150
- <merge xmlns:android="http://schemas.android.com/apk/res/android">
- <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" />
- </Merge>
The effect shown in emulator after running the program is the same, but the UI structure viewed through hierarchyviewer is changed, and the redundant framelayout nodes are merged together. Or it can be understood that the subset of the merge tag is added directly to the activity's framelayout and nodes (it is important to note that the root node of all activity Views is framelayout). If you create a layout that does not use framlayout as the root node (instead of defining root tags such as linerlayout), you cannot use the above example to optimize the UI structure by using merge.
4. In addition to the above example, Meger has another usage
When an include or viewstub tag is imported from an external XML structure, the imported XML can be represented as a root node, so that when it is embedded in the parent structure it is good to dissolve the subset it contains into the parent structure without the redundant nodes.
There are two additional points that require special attention:
- <merge/> can only be used as the root node of XML layout.
- When the XML layout that needs to be expanded is itself composed of the merge as the root node, the imported XML layout needs to be placed in the ViewGroup, and the attachtoroot must be set to true. (See inflate () documentation for more instructions)
Use of the merge and include tags in android