MasterofProject

Performance optimization of Android application development 3:merge Tags

Classification: Android finishing 322 people read Comment(0) Collection Report
Before in the development of I have not used merge this label, and later on by my colleagues to me, I tried to use the merge label, the label is very helpful for the application of layout optimization. Today is simply about the use of the label. When it comes to <merge > tag to the application of optimization is mainly optimized UI structure by deleting superfluous or extra level of hierarchy, to optimize the Android in the layout of the layout of the structure.

Will be through an example to understand the actual role of the label, so that more intuitive understanding of the use of <merge/>. But we need to pay attention to the use of the time:

1, <merge / > only as the root node of the layout in XML file content, like the merge for we usually use the root node LinearLayou, relativelayout, etc..
2, if you need to expand the layout layout of the XML file itself is by merge as a root node, then you need to be imported into the layout layout (outside the set of a ViewGroup viewGroup control), and the need to set the property True attachToRoot.

3, the simplest and most commonly usedUsage: if we use the FrameLayout as activity's content view of the parent element (that is, in the main.xml put it down in the outermost layer), so can consider to replace the <FrameLayout > <merge >, which can reduce a layer structure to achieve the optimization effect.

The following code is a simple Layout layout, containing two Views elements: ImageView and TextView. By default, we put the two elements in FrameLayout: a picture is displayed in the main view, then the caption is displayed on the picture, and it is located in the bottom of the view:
<? Version= encoding= "1" XML "UTF-8" >
Xmlns:android= http://schemas.android.com/apk/res/android "<FrameLayout"
Fill_parent "android:layout_width="
Android:layout_height= "fill_parent" >
 
<ImageView
Match_parent "android:layout_width="
Match_parent "android:layout_height="
Center "android:scaleType="
Android:src= "@drawable/my_back" / >
 
<TextView
Wrap_content "android:layout_width="
Wrap_content "android:layout_height="
20dip "android:layout_marginBottom="
Center_horizontal|bottom "android:layout_gravity="
10dp "android:padding="
#AA000000 "android:background="

#ff00ff "android:textColor="

16sp "android:textSize="

Android:text= "MERGE label" / >
      
</FrameLayout>
We directly replace the XML code in the top of the framLayout merge:
<? Version= encoding= "1" XML "UTF-8" >
Xmlns:android= <merge "http://schemas.android.com/apk/res/android" >
 
<ImageView
Match_parent "android:layout_width="
Match_parent "android:layout_height="
Center "android:scaleType="
Android:src= "@drawable/my_back" / >
 
<TextView
Wrap_content "android:layout_width="
Wrap_content "android:layout_height="
20dip "android:layout_marginBottom="
Center_horizontal|bottom "android:layout_gravity="
12dip "android:padding="
#AA000000 "android:background="

#ff00ff "android:textColor="

16sp "android:textSize="

Android:text= "MERGE label" / >
      
</merge>
This can be, after running the program to display the effect is the same, but through the UI hierarchyviewer structure is a change, the original excess FrameLayout nodes are merged together, or can be understood as a subset of the merge tag directly added to the FrameLayout Activity node (here need to remind you: all Activity views of the root node is frameLayout). If you are not using Layout as a root node (instead of using LinerLayout to define the root tag), you can not use the above example to optimize the UI structure by merge.

 

Copyright statement: This article is the original article of the main Bo, without the permission of the owner.

Top
One
Step
0
Guess what you're looking for.
View comments
* above the user's comments only represent their personal views, do not represent the views or position of the CSDN website
    Personal data
    • Visit35945 times
    • Integral:1739
    • Grade
    • Rank:13153rd
    • Original144
    • Reprint:36
    • Translation:1
    • Comment:9
    Latest comments