Android performance optimization 4 Layout Optimization use of merge tags, androidmerge

Source: Internet
Author: User

Android performance optimization 4 Layout Optimization use of merge tags, androidmerge


TIPS: I previously shared the use of the ViewStub label. Is there any other Layout Optimization Method for Android?
Blacklist: The <merge/> label is used to reduce the View tree hierarchy to optimize the layout of Android. Here is an 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 tag usage"/> </RelativeLayout>


The simplest Activity is named 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);     }}



TIPS: follow the code above to create a project. After running the project, use the "DDMS-> Dump View Hierarchy for UI Automator" tool as follows:
Before using merge


Blacklist: the bottom two layers of RelativeLayout and TextView are the content in the activity_main.xml layout. The above FrameLayout is the top-level view added by Activity setContentView. The following uses the merge tag to view the differences

The layout file activity_main.xml is 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 tag usage"/> </merge>



TIPS: use the "DDMS-> Dump View Hierarchy for UI Automator" tool as follows:
After merge is used



Small Black: TextView is directly under FrameLayout, which has the same effect as RelativeLayout.




In some cases, custom reusable la s contain too many hierarchical labels, such
For example, when <include> is used to include the preceding layout, the system automatically ignores the merge level and places the two buttons directly at the same level as the include level.



TIPS: What is Merge label used?
Small Black: like the above example, the Child view does not need to specify any layout attribute for the parent view. In this example, TextView only needs to be directly added to the parent view for display.
In addition, if a layout (or view) needs to be embedded in LinearLayout, and the root node of the layout (or view) is also LinearLayout, a layer of unused Nesting is added, undoubtedly, this will only slow down the program speed. At this time, if we use the merge root tag, we can avoid that problem. This is the case demonstrated in the official Android Layout Tricks #3: Optimize by merging example.
TIPS: <merge/> Are there any restrictions on labels?
Blacklist: <merge/> can only be used as the root label of the XML layout. When an Inflate layout file starts with <merge/>, you must specify a parent ViewGroup and set attachToRoot to true.

Small Black: merge labels also have some attributes available for use. For details, refer to the API documentation, such as android: layout_width and android: layout_height.

References:Re-using Layouts with <include/> Android Layout Tricks #2: Reusing layouts include and merge labels
Android Layout Tricks #3: Optimize by merging Android Application Performance Optimization Chapter 1 graphic Performance Optimization Layout Optimization Merge source code


For more Optimization-related articles, see section 3 Performance Optimization in summary of Android basic learning articles.




If God gives you two choices

Of course, select the second one!
With countless money, many people will come up with this idea, and now people live to make more money, in order to better survive. However, it would be a pity to choose the first one with only 10 years of service life. There are a lot of fun in life that have not yet been enjoyed, and there are still many things that have not been done, with family, it will make the lover suffer. how miserable it is when his parents are black-haired and white-haired? Being so cruel for money cannot be tolerated or forgiven!

How can we improve and improve the overall layout of Android?

If the implementation is poor, your layout may lead to a memory-occupied application and slow response user interface. Android SDK helps you identify layout performance problems. When the following tutorial is integrated, You can implement a smooth scrolling interface with low memory usage. In the tutorial, optimizing the structure layout * is like adding loading time to a complex webpage. If your layout hierarchy is too complex, it may also cause performance problems. This tutorial will show you how to use the SDK tool to check your layout and discover performance bottlenecks. Re-use Layout * if your application UI repeatedly uses some layout structures in multiple places, this tutorial shows you how to create an efficient and reusable layout structure, then include them in the appropriate UI layout. Loading a view as needed * apart from simply adding a layout component to another layout, you may only want to make the included layout visible when needed, such as after the activity runs for a period of time. This tutorial shows how to improve the initialization performance of your layout by loading some requirements on your layout. Smooth ListView scrolling * If you have created a content instance ListView that contains complex or a large amount of data in each list item, the scrolling performance may be affected. This tutorial provides some tips on how to make your rolling performance smoother.

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.