Android performance Optimization Four layouts optimize the use of merge tags

Source: Internet
Author: User



Small white: Previously shared the use of Viewstub tags, Android and other ways to optimize the layout?
Small black: <merge/> Tags are used to reduce the level of the view tree to optimize the Android layout. Let's start with an example to illustrate:


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 the project according to the code above, and use the "DDMS-and-Dump View Hierarchy for UI Automator" tool after running, as follows
Merge before use


Little black: The bottom two layers relativelayout and TextView are the contents of the Activity_main.xml layout, the framelayout above is the top view added by activity Setcontentview. Use the Merge tab below to see the difference

The layout file Activity_main.xml modified as follows:
<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, as follows
After the merge is used



Small black: Framelayout below is directly textview, compared with the previous one layer relativelayout and achieve the same effect.




At some point, a custom reusable layout contains too many levels of labels, such as our
For example, if you use <include> to include the above layout, the system automatically ignores the merge level, and the two buttons are placed directly with the include peer



Small white: What is the case for using the merge tag?
Black: One is like the above example, the child view does not need to specify any layout properties for the parent view, in the example TextView just needs to be added directly to the parent view for display on the line.
The other is that 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 unused nesting, it is no doubt that this only slows down the program speed. And at this point, if we use the merge root tag to avoid that problem, the official document Android Layout Tricks #3: Optimize by the example in merging demonstrates this situation.
Small white: <merge/> Label What's the limit?
Black: <merge/> can only be used as the root tag of an XML layout. When you inflate a layout file that begins with <merge/>, you must specify a parent ViewGroup, and you must set Attachtoroot to True.

Black: The merge tag also has some properties to use, so you can view the 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 graphic performance optimization of the merge source code


for more optimization related articles, see:Android Basic Learning Article Summary"Part III performance optimization



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.