Small white: Previously shared the use of viewstub tags. Is there any other way to optimize the layout of Android?
Black: <merge/> Tags are used to reduce the level of the view tree to optimize the Android layout. Let's start with a sample example:
First, the master needs a configuration file Activity_main.xml
<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools " android:layout_width=" match_parent " android:layout_height=" Match_parent " > <textview android:layout_width= "wrap_content" android:layout_height= "Wrap_content " android:text= "Merge label use"/></relativelayout>
One more simple activity, file name Mainactivity.java
Package Com.example.merge;import Android.app.activity;import Android.os.bundle;public class MainActivity extends Activity { @Override protected void onCreate (Bundle savedinstancestate) { super.oncreate ( Savedinstancestate); Setcontentview (R.layout.activity_main); }}
Small white: Create project According to the code above, and then use the DDMS, Dump View Hierarchy for UI Automator tool, such as the following
Merge before use
Black: The two-storey relativelayout and TextView are the contents of the Activity_main.xml layout, and the framelayout above is the top-level view that activity Setcontentview joins. Use the Merge tab below to see the difference
The layout file activity_main.xml changes such as the following:
<merge xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools " android:layout_width=" match_parent " android:layout_height=" Match_parent " > <textview android:layout_width= "wrap_content" android:layout_height= "Wrap_content " android:text= "Merge label use"/></merge>
Small white: Use the DDMS, Dump View Hierarchy for UI Automator tool. For example, the following
After the merge is used
Small black: Framelayout The following is directly textview, compared with the previous one less layer relativelayout and achieve the same effect.
At some point, you define reusable layouts that include too many levels of labels, for example, we
For example: such words. Use <include> to include the above layout. The system will proactively ignore the merge hierarchy itself. and put two buttons directly with the include peer
Small white: What is the case for using the merge tag?
Small black: One is the same as the example above. The child view does not need to specify any layout properties for the parent view, in which case the TextView only needs to be added directly to the parent view for display.
The second is if you need to embed a layout (or view) inside the LinearLayout, and the root node of the layout (or view) is also linearlayout, so that there is a layer of not practical nesting, it is no doubt that this will only slow down the program speed. And this time assuming that we can avoid that problem with the merge root tag, the official document Android Layout Tricks #3: This is the case shown in the sample Optimize by merging.
Small white: <merge/> Label What's the limit?
Small black: <merge/> can only be used as the root tag of an XML layout.
When inflate a layout file that starts with <merge/>. You must specify a parent ViewGroup, and you must set Attachtoroot to True.
Small black: Merge tag other properties are available for use. Detailed ability to view API documentation, such asAndroid:layout_width,Android:layout_height, etc.
References :re-using Layouts with <include/>android Layout Tricks #2: Reusing Layoutsinclude and merge tags use examples
Android layout Tricks #3: Optimize by merging "Android Application performance Optimization" 8th. Optimization of graphics performance optimization of the layout of the merge source code
many other optimization related articles are detailed in:Android Basic Learning Article Summary"Part III performance optimization
Android performance Optimization Four layouts optimize the use of merge tags