콘텐츠로 건너뛰기

자주 방문한 페이지

최근 방문한 페이지

navigation

Optimizing Layout Hierarchies

This lesson teaches you to

  1. Inspect Your Layout
  2. Revise Your Layout
  3. Use Lint

You should also read

It is a common misconception that using the basic layout structures leads to the most efficient layouts. However, each widget and layout you add to your application requires initialization, layout, and drawing. For example, using nested instances of LinearLayout can lead to an excessively deep view hierarchy. Furthermore, nesting several instances of LinearLayout that use the layout_weight parameter can be especially expensive as each child needs to be measured twice. This is particularly important when the layout is inflated repeatedly, such as when used in a ListView or GridView.

In this lesson you'll learn to use Hierarchy Viewer and Lint to examine and optimize your layout.

Inspect Your Layout

The Android SDK tools include a tool called Hierarchy Viewer that allows you to analyze your layout while your application is running. Using this tool helps you discover bottlenecks in the layout performance.

Hierarchy Viewer works by allowing you to select running processes on a connected device or emulator, then display the layout tree. The traffic lights on each block represent its Measure, Layout and Draw performance, helping you identify potential issues.

For example, figure 1 shows a layout that's used as an item in a ListView. This layout shows a small bitmap image on the left and two stacked items of text on the right. It is especially important that layouts that will be inflated multiple times—such as this one—are optimized as the performance benefits will be multiplied.

Figure 1. Conceptual layout for an item in a ListView.

Hierarchy Viewer shows a list of available devices and their running components. Choose your component from the Windows tab, and click Hierarchy Viewto view the layout hierarchy of the selected component. For example, figure 2 shows the layout for the list item illustrated by figure 1.

Figure 2. Layout hierarchy for the layout in figure 1, using nested instances of LinearLayout.

Revise Your Layout

Because the layout performance above slows down due to a nested LinearLayout, the performance might improve by flattening the layout—make the layout shallow and wide, rather than narrow and deep. A RelativeLayout as the root node allows for such layouts. So, when this design is converted to use RelativeLayout, you can see that the layout becomes a 2-level hierarchy. Inspection of the new layout looks like this:

Figure 4. Layout hierarchy for the layout in figure 1, using RelativeLayout.

The benefits, as small as they might seem, are multiplied several times because this layout is used for every item in a list.

Most of the difference is due to the use of layout_weight in the LinearLayout design, which can slow down the speed of measurement. It is just one example of how each layout has appropriate uses and you should carefully consider whether using layout weight is necessary.

In some complex layouts, the system may waste effort measuring the same UI element more than once. This phenomenon is called double taxation. For more information about double taxation and how to prevent it, see Performance and View Hierarchies.

Use Lint

It is always good practice to run the lint tool on your layout files to search for possible view hierarchy optimizations. Lint has replaced the Layoutopt tool and has much greater functionality. Some examples of lint rules are:

Another benefit of Lint is that it is integrated into Android Studio. Lint automatically runs whenever you compile your program. With Android Studio, you can also run lint inspections for a specific build variant, or for all build variants.

You can also manage inspection profiles and configure inspections within Android Studio with the File>Settings>Project Settings option. The Inspection Configuration page appears with the supported inspections.

Figure 5. Inspection Configuration

Lint has the ability to automatically fix some issues, provide suggestions for others and jump directly to the offending code for review.

이 사이트는 쿠키를 사용하여 사이트별 언어 및 표시 옵션에 대한 환경설정을 저장합니다.

Google Play에서 성공을 거두는 데 도움이 되는 최신 Android 개발자 뉴스 및 도움말을 받아 보세요.

* 필수 입력란

완료되었습니다.

WeChat에서 Google Developers 팔로우하기

이 사이트를 (으)로 탐색할까요?

페이지를 요청했지만 이 사이트의 언어 환경설정은 입니다.

언어 환경설정을 변경하고 이 사이트를 (으)로 탐색할까요? 언어 환경설정을 나중에 변경하려면 각 페이지 하단의 언어 메뉴를 사용하세요.

이 클래스를 사용하려면 API 수준 이상이 필요합니다.

문서에 대해 선택한 API 수준이 이므로 이 문서가 표시되지 않습니다. 왼쪽 탐색 메뉴의 선택기로 문서 API 수준을 변경할 수 있습니다.

앱에 필요한 API 수준 지정에 관한 자세한 내용은 다양한 플랫폼 버전 지원을 참조하세요.

Take a short survey?
Help us improve the Android developer experience. (April 2018 — Developer Survey)