in our development of the app not only in the code implementation, to optimize the app, and in our interface layout there are many places to optimize, if the layout is very low, the system load layout is very slow, so that the user experience is very bad, This article is mainly from my usual layout of the optimization of the summary, I think often can be used in the layout optimization of some of the techniques and methods.
1. Reduce the nesting of layouts, which is also the most important
Android all know, Android's entire UI layout file at the end of a layer of parsing into a View object, if 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, the usual means are: 1) use Relativelayout instead of linearlayout. 2) When you have finished writing the layout file, you can use the Hieracyview tool to check for unwanted layouts, and if so, you must remove the unused 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, but strictly speaking, layout reuse simply reduces the amount of code we write and does not optimize the application, and When using include, it is easy to produce a useless parent layout in the 1th, such as:
How to remove the negative effects of using the include generation, there are scenarios below.
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, combining 2 and 32 points to see an example usage:
First, create a merge label root layout with the following code:
? 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"/>
Next, use the include to introduce the layout:
<include layout= "@layout/ok_cancel_layout"/>
4. Use the Viewstub delay to load some less-than-used layouts, replacing the use of android:visibility= "gone" on some occasions, because the layout that is gone out is constantly creating objects at the same time. Then why use viewstub efficient, take the source to speak, first look at Viewstub Source:
@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>
As can be seen by the Onmeasure () method and the Draw () method, the initial width of the viewstub is zero, so he does not take up space, and then the draw () method does not perform any of the drawing, the two methods can be seen, viewstub is indeed very efficient.
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 background repainting of different layers, for example, if the background of a view parent layout is already set to white, you do not need to set the background color for this view, and with this simple trick, you can increase the speed very much.
6. If the entire app uses custom title and background, then we can use a custom style to permanently remove the title and background that the system has added to our activity by default, You can also increase the rendering speed of your activity.
Optimized layout optimization for Android applications