Optimized layout optimization for Android applications

Source: Internet
Author: User

when we develop the app, it's not just about the code implementation. To optimize the app, and in our interface layout there are a lot to optimize, assuming that the layout is very low, the system loading layout is very slow, making the user experience is very bad. This article is mainly from my usual layout of the optimization of the summary, I feel often able to use the layout optimization of some of the techniques and methods.

1. Reduce the nesting of layouts. And that's the most important thing.

Android all know, Android's entire UI layout file at the end of a layer of parsing into a view object, assuming that the level is too deep, to lead to recursive hierarchy too deep and greatly affect the resolution speed, so we must not let the layout file hierarchy too deep, To achieve the layout of the file hierarchy is not deep, commonly used by means of: 1) Use Relativelayout to replace LinearLayout. 2) When you have finished writing the layout file. Be able to use the Hieracyview tool to check if there is superfluous layout, fake, you must remove the useless layout.

2. Layout reuse.

Some common layouts we don't have to rewrite them every time, write them as a separate layout file, and finally use the include tag to refer to the layout. However, strictly speaking, layout reuse is only to reduce the amount of code we write, and can not achieve the corresponding optimization role. Also, when using include, it is easy to produce a useless parent layout in the 1th, such as:

How to remove the negative effect of using include generation, the following will be a solution.

3. Use the Merge tab to eliminate the unwanted layout introduced by the Include tag

When using merge, be aware that the merge tag can only be a root layout, combined with 2. 32-point look at a demo sample how to use:

First, create a merge tag with the root layout, code such as the following:  

   ?

XML version= "1.0" encoding= "Utf-8"?> <merge xmlns:android= "Http://schemas.android.com/apk/res/android" > <button android:id= "@+id/ok" android:layout_width= "match_parent" android:layout_height = "Wrap_content" android:layout_marginleft= "20DP" android:layout_marginright= "20DP" android:text= " Ok "/> <button android:id=" @+id/cancel " android:layout_width=" Match_parent " android: layout_height= "Wrap_content" android:layout_marginleft= "20DP" android:layout_marginright= "20DP" android:layout_margintop= "10DP" android:text= "Cancel"/>

Then. To introduce a layout using include:

   <include layout= "@layout/ok_cancel_layout"/>
4. Use viewstub delay to load some unused layout, in some cases instead of android:visibility= "Gone" method. Because the layout that was gone out is constantly creating objects at the same time. Then why use viewstub efficient, take the source code to speak, first look at the Viewstub source code:      

       @Override       protected void onmeasure (int widthmeasurespec, int heightmeasurespec) {          setmeasureddimension (0, 0);        }<span style= "font-family:arial, Helvetica, Sans-serif;" >       </span>
<p style= "margin-top:0px; margin-bottom:0px; font-size:11px; Font-family:monaco; Color:rgb (119, 119, 119); " ><span style= "Color:rgb (0, 0, 0); >      </span> @Override </p><p style= "margin-top:0px; margin-bottom:0px; font-size:11px; Font-family:monaco; " >     <span style= "color: #931a68" >public</span> <span style= "color: #931a68" >void</span > Draw (Canvas <span style= "color: #7e504f" >canvas</span>) {</p><p style= "margin-top:0px; margin-bottom:0px; font-size:11px; Font-family:monaco; " >     }</p>

Can be seen by the Onmeasure () method and the Draw () method. The initial width and height of the viewstub are zero, so he does not take up space at first, and the draw () method does not run no matter what the drawing, which can be seen by both methods. Viewstub is very efficient indeed.

To manipulate viewstub in your code, first use the Viewstub.inflate () method to initialize the view that it owns. Otherwise, a null pointer error is reported.

5. Reduce the background redraw between different layers. For example, suppose that the background of a view parent layout is already set to white. There is no need to set the background color for this view, and with this simple trick, there is a lot of lifting speed.

6. Assuming that the entire app uses its own defined title and background, then we can use our own defined style to permanently remove the title and background that our activity has added to the system, It can also improve the rendering speed of the activity.


Optimized layout optimization for Android applications

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.