[Andorid Application Development]-(4) Layout Optimization Principles and Methods

Source: Internet
Author: User

The performance optimization mentioned above is mainly for Java code optimization. This article shares the layout optimization. The layout optimization is based on the following principles:

1. Avoid unnecessary nesting. Do not place a layout in other la s unless necessary;

2. Avoid too many attempts. adding a new view in a layout will consume time and resources in the inflate operation. Do not include more than 80 views in a layout at any time. Otherwise, the time consumed by the inflate operation will be large.

3. Avoid deep nesting and the layout can be nested at will, which makes it easy to create complex and deep nested layout layers. If there are no hardware restrictions, it is best to limit nesting to less than 10 layers.

From the above three optimization principles, we can conclude that layout optimization mainly involves depth and breadth. Depth mainly involves nested layout usage, and breadth mainly includes excessive views.

I don't know how to obtain data with more than 80 views and 10 layers in three principles. However, during the development process, the number of custom views displayed by a single Activity does not exceed 20, and the number of layers does not exceed 4. I personally think that if there are more than 20 views for a project that is not too big or special, the 4-layer design must be optimized. For example, to design a dial-up interface, which contains 0-9, *, #, and other characters, is implemented by nested buttons Based on LineaLayout? Or use a GridView to reduce the number of buttons and thus reduce redundant code? I suggest using the GridView to implement ~

In-depth optimization is also mentioned. It is necessary to use the hierarchyviewer tool provided by the Android SDK to analyze the problem of deep optimization. The following uses HelloWorld to analyze the layout structure.

Create a new project without any changes. Use hierarchyviewer to view the layout structure (hierarchyviewer is easy to use. Run the simulator or the real machine to connect to ADB and open hierarchyviewer to find the package name of the project)

 

 

Does a HelloWorld program with no changes have such a multi-level structure? Yes, the layout we want to do is the red part. We can see that all custom la s inherit FrameLayout. The so-called deep optimization starts from here. The layout optimization mainly starts from the label. The following three labels are introduced: <merge>, <ViewStub>, <include>

1. Merge: The merge label is mainly used to reduce the View tree hierarchy for optimization. See a simple layout code.


[Html]
<SPAN style = "FONT-SIZE: 16px"> <FrameLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: orientation = "vertical">
<TextView
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "@ string/hello"/>
</FrameLayout> </SPAN>

<FrameLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: orientation = "vertical">
<TextView
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "@ string/hello"/>
</FrameLayout>
Structure chart

 


Through the tree structure displayed by hierarchyviewer, we can see that the custom Layout FrameLayout view inherits from the Framelayout layout. However, the custom FrameLayout does not actually play any role here, so it is good, since merge is used to reduce the View tree hierarchy for optimization, what is the effect of changing to the merge tag?

The Code is as follows:

[Html]
<Merge xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: orientation = "vertical">
<TextView
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "@ string/hello"/>
</Merge>

<Merge xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: orientation = "vertical">
<TextView
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "@ string/hello"/>
</Merge>
Structure:

 


It is a good method to effectively optimize the layout by modifying the layout.

2. ViewStub: The biggest function of this label is that all Views contained by it do not occupy any memory space by default, and it is invisible by default.


3. Include: You can use this label to directly load external xml into the current structure, which is a common tag for reusing UI resources.

 

Author: tangcheng_ OK
 

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.