Android Application development performance optimization 3: merge tag, androidmerge

Source: Internet
Author: User

Android Application development performance optimization 3: merge tag, androidmerge
I have never used the merge tag during development. After my colleagues told me about it, I tried to use the merge tag, it is found that this label is indeed very helpful for application layout optimization. Today, I will briefly describe the usage of this label. Speaking of <merge/> tag Optimization for applications, the UI structure is optimized mainly by deleting redundant or additional layers to optimize the Layout structure of the entire Android application.

We will use an example to understand the actual functions of this tag, so that we can more intuitively understand the usage of <merge/>. However, you must note the following when using it:

1. <merge/> can only be used as the root node of xml file content in layout, just like replacing the root node LinearLayou and RelativeLayout we usually use with merge.
2. If the xml file in the layout to be expanded uses merge as the root node, You need to place the imported layout in the viewGroup (A ViewGroup control is set outside ), at the same time, set the attribute attachToRoot to True.

3. The simplest and most common usage: If we use FrameLayout as the parent element of the activity's content view (that is, in main. in xml, you can use <merge/> to replace <FrameLayout/> to reduce a layer of structure to achieve optimization.

The following code is a simple Layout with two Views elements: ImageView and TextView. By default, we place the two elements in FrameLayout: display an image in full screen in the main view, and then display the title on the image, which is located below the View:
<? Xml version = "1.0" encoding = "UTF-8"?>
<FrameLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent">
 
<ImageView
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"
Android: scaleType = "center"
Android: src = "@ drawable/my_back"/>
 
<TextView
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_marginBottom = "20dip"
Android: layout_gravity = "center_horizontal | bottom"
Android: padding = "10dp"
Android: background = "# AA000000"

Android: textColor = "# ff00ff"

Android: textSize = "16sp"

Android: text = "use MERGE labels"/>

</FrameLayout>
We replace framLayout in the above xml code with merge:
<? Xml version = "1.0" encoding = "UTF-8"?>
<Merge xmlns: android = "http://schemas.android.com/apk/res/android">
 
<ImageView
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"
Android: scaleType = "center"
Android: src = "@ drawable/my_back"/>
 
<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 = "# ff00ff"

Android: textSize = "16sp"

Android: text = "use MERGE labels"/>

</Merge>
In this way, the Emulator displays the same effect after running the program. However, the UI structure viewed through hierarchyviewer has changed, and the redundant FrameLayout nodes have been merged, you can also add a subset of the merge label to the FrameLayout and node of the Activity directly. (Note that 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.

 

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.