BKJIA: creating good-looking Android la S is not a big challenge. After you spend hours adjusting them to adapt to multiple devices, you usually don't want to adjust them any more, however, the bulky nested layout is often very inefficient. Fortunately, there is a tool in the Android SDK that helps you optimize the layout to reduce memory consumption and improve the application running performance.
Layoutoptimization
Optimization requires some skills. Code with good performance is important, but the cost of writing good code is usually high, you may not rush to optimize code that runs only once or temporarily. If your application is unresponsive and expensive, or slows down other applications in the system, the user will respond, and your application downloads will be affected.
Optimizing your layout as early as possible during development is a simple way to save costs and improve performance. The Android SDK provides a tool that automatically analyzes your layout, discover unnecessary layout elements to reduce layout complexity.
Step 1: Prepare
If you want to use the optimization tool provided by the Android SDK, you need to work in the command line of the development system. If you are not familiar with the command line tool, you have to work hard.
We strongly recommend that you add the path of the Android tool to the environment variable of the operating system, so that you can directly name the tool and run it, otherwise, enter the complete file path at the end of the command prompt. Now there are two tool directories in Android SDK:/tools and/platform-tools, this article mainly uses the layoutopt tool in the/tools directory. In addition, I want to say that the ADB tool is located in the/platform-tools directory.
Run layoutopt
Running the layoutopt tool is quite simple. You only need to keep up with the directory where the layout file or layout file is located as a parameter. Note that you must include the complete path of the layout file or directory, even if you are currently in this directory. Let's take a simple example:
- D:\d\tools\eclipse\article_ws\Nothing\res\layout>layoutopt D:\d\tools\eclipse\article_ws\Nothing\res\layout\main.xml
- D:\d\tools\eclipse\article_ws\Nothing\res\layout\main.xml
- D:\d\tools\eclipse\article_ws\Nothing\res\layout>
Note: In the preceding example, the complete path of the file is included. If the complete path is not specified, NO content is output. For example:
- D:\d\tools\eclipse\article_ws\Nothing\res\layout>layoutopt main.xml
- D:\d\tools\eclipse\article_ws\Nothing\res\layout>
Therefore, if you do not see anything, it is likely that the file is not parsed, that is, the file may not be found.
Layoutopt output
Layoutopt's output result is just a suggestion. You can choose to adopt these suggestions in your application. Here are some examples of layoutopt's output suggestions.
Useless Layout
During the layout design, we will move various components frequently, and some components may not be used any more, such:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="horizontal">
- <LinearLayout
- android:id="@+id/linearLayout1"
- android:layout_height="wrap_content"
- android:layout_width="wrap_content"
- android:orientation="vertical">
- <TextView
- android:id="@+id/textView1"
- android:layout_width="wrap_content"
- android:text="TextView"
- android:layout_height="wrap_content"></TextView>
- </LinearLayout>
- </LinearLayout>
The tool will soon tell us that the LinearLayout in LinearLayout is redundant:
- 11:17 This LinearLayout layout or its LinearLayout parent is useless
The two numbers at the top of each row in the output result indicate the suggested row number.
Root replacement
Layoutopt output is sometimes contradictory, for example:
- <?xml version="1.0" encoding="utf-8"?>
- <FrameLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent">
- <LinearLayout
- android:id="@+id/linearLayout1"
- android:layout_height="wrap_content"
- android:layout_width="wrap_content"
- android:orientation="vertical">
- <TextView
- android:id="@+id/textView1"
- android:layout_width="wrap_content"
- android:text="TextView"
- android:layout_height="wrap_content"></TextView>
- <TextView
- android:text="TextView"
- android:id="@+id/textView2"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"></TextView>
- </LinearLayout>
- </FrameLayout>
This layout will return the following output:
- 5:22 The root-level <FrameLayout/> can be replaced with <merge/>
- 10:21 This LinearLayout layout or its FrameLayout parent is useless
Although the suggestions in the first line are feasible, they are not necessary. We want the two textviews to be placed vertically, so LinearLayout should be retained, while the suggestions in the second line can be adopted to delete useless FrameLayout.
Interestingly, this tool is not all-powerful. For example, in the above example, if we add a background attribute to FrameLayout and then run the tool, the first suggestion will certainly disappear, however, the second suggestion will still show that the tool knows that we cannot control the background through merging, but after checking the LinearLayout, it seems that we forgot to add a property not provided by LinearLayout to FrameLayout.
Too many views
Each view consumes memory. If too many views are arranged in one layout, the layout occupies too much memory. If a layout contains more than 80 views, layoutopt may give the following suggestions:
- -1:-1 This layout has too many views: 83 views, it should have <= 80!
- -1:-1 This layout has too many views: 82 views, it should have <= 80!
- -1:-1 This layout has too many views: 81 views, it should have <= 80!
The suggestion above is that the number of views cannot exceed 80. Of course, the latest device may support so many views. However, if the performance is poor, you 'd better adopt this suggestion.
Too many nesting
Layoutopt and the Android team recommend that the layout be within 10 levels. The layout should not exceed 10 levels even for the largest tablet screen, relativeLayout may be a solution, but its usage is more complex. It is better to make it easier after the Graphical Layout resource tool in Eclipse is updated.
The following figure shows the output content of layoutopt when there are too many layoutopt la s:
- -1:-1 This layout has too many nested layouts: 12 levels, it should have <= 10!
- 305:318 This LinearLayout layout or its RelativeLayout parent is possibly useless
- 307:314 This LinearLayout layout or its FrameLayout parent is possibly useless
- 310:312 This LinearLayout layout or its LinearLayout parent is possibly useless
Nested layout warnings are usually accompanied by warnings of useless la S, which help to identify which la s can be removed and avoid all la s being redesigned.
Summary
Layoutopt is a quick and easy-to-use layout analysis tool to find out inefficient and useless la s. What you need to do is to determine whether to adopt the optimization suggestions provided by layoutopt, although the adoption of recommendations to make changes will not immediately significantly improve performance, there is no reason to slow down the entire application with complicated la S, and the later maintenance is difficult. The simple layout not only simplifies the development cycle, but also reduces the test and maintenance workload. Therefore, during application development, you should optimize your layout as soon as possible, and do not wait until the user feedback is returned for modification.
Original article name: Android SDK Tools: Layout Optimization Author: Shane Conder and Lauren Darcey Original article
BKJIA translations are not reprinted without authorization. For reprinted by the media partners, please indicate the source, author, and BKJIA translations and translators !]