Android UI in-depth Optimization of merge labels

Source: Internet
Author: User

Android memory optimization is a very important issue, and UI optimization is a top priority.

This label plays an important role in optimizing the UI structure. It aims to optimize the entire UI Layout structure by deleting redundant or additional layers. Create a simple Layout with two Views elements: ImageView and TextView. By default, these two elements are placed in FrameLayout. The effect is to display an image in full screen in the main view, and then display the title on the image, which is located below the view. The following is the xml code:
Xml Code
<FrameLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent">
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: scaleType = "center"
Android: src = "@ drawable/golden_gate"/>

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>

Start tools> hierarchyviewer. bat to view the current UI Structure View:


We can see that there are two framelayout nodes in the structure contained by the red box. Obviously, the two nodes with the same meaning cause a waste of resources, in this case, you can use the merge label to solve the problem. Replace the root node with the merge label as follows:
Xml Code
<Merge xmlns: android = "http://schemas.android.com/apk/res/android">
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: scaleType = "center"
Android: src = "@ drawable/golden_gate"/>
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"/>

After running the program, the Emulator displays the same effect. However, the UI structure viewed through hierarchyviewer has changed. The redundant FrameLayout nodes were merged, you can also add a subset of the merge tag to the FrameLayout root node of the Activity. (Note: 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.
In addition to the preceding example, meger also has another usage: When the Include or ViewStub label is used to import the xml structure from outside, merge can be used as the root node for the imported xml, in this way, after being embedded into the parent structure, the subset it contains can be well integrated into the parent structure without redundant nodes.
Pay special attention to the following two points:
A. It can only be used as the root node of xml layout;
B. If the xml layout to be expanded is based on merge as the root node, You need to place the imported xml layout in the viewGroup, and set attachToRoot to True.


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.