[null,null,["最后更新时间 (UTC):2025-07-27。"],[],[],null,["# Optimize layout hierarchies\n\nTry the Compose way \nJetpack Compose is the recommended UI toolkit for Android. Learn how to work with layouts in Compose. \n[Layout Inspector in Compose →](/jetpack/compose/tooling/layout-inspector) \n\nIt's a common misconception that using the basic layout structures leads to\nthe most efficient layouts. However, each widget and layout you add to your app\nrequires initialization, layout, and drawing. For example, using nested\ninstances of\n[LinearLayout](/reference/android/widget/LinearLayout)\ncan lead to an excessively deep view hierarchy. Furthermore, nesting several\ninstances of `LinearLayout` that use the `layout_weight`\nparameter can be especially expensive, as each child needs to be measured twice.\nThis is particularly important when the layout is inflated repeatedly, such as\nwhen used in a\n[RecyclerView](/reference/androidx/recyclerview/widget/RecyclerView).\n\nThis document shows how to use\n[Layout Inspector](/studio/debug/layout-inspector) and\n[lint](/studio/write/lint) to examine and optimize your layout.\n\nInspect your layout\n-------------------\n\nThe Android SDK tools includes the\n[Layout Inspector](/studio/debug/layout-inspector) tool, which lets\nyou analyze your layout while your app is running. Using this tool helps you\ndiscover inefficiencies in the layout performance.\n\nLayout Inspector lets you select running processes on a connected device or\nemulator, then display the layout tree. The traffic lights on each block\nrepresent its Measure, Layout, and Draw performance, helping you identify\npotential issues.\n\nFor example, figure 1 shows a layout that's used as an item in a\n`RecyclerView`. This layout shows a small bitmap image on the left\nand two stacked items of text on the right. It is especially important that\nlayouts like this that are inflated multiple times are optimized, as the\nperformance benefits are multiplied.\n**Figure 1.** Conceptual layout for an item in a [RecyclerView](/reference/androidx/recyclerview/widget/RecyclerView).\n\nLayout Inspector shows a list of available devices and their running\ncomponents. Choose your component from the **Windows** tab, and click\n**Layout Inspector** to view the layout hierarchy of the selected component.\nFor example, figure 2 shows the layout for the list item illustrated by figure\n1.\n**Figure 2.** Layout hierarchy for the layout in figure 1, using nested instances of `LinearLayout`.\n\nRevise your layout\n------------------\n\nBecause the preceding layout performance slows down due to a nested\n`LinearLayout`, you might improve performance by flattening the\nlayout---in other words, making the layout shallow and wide, rather than\nnarrow and deep. A\n[ConstraintLayout](/develop/ui/views/layout/constraint-layout)\nas the root node allows for such layouts. When you convert this design to use\n`ConstraintLayout`, the layout becomes a two-level hierarchy: \n\n```xml\n \u003candroidx.constraintlayout.widget.ConstraintLayout\n xmlns:android=\"http://schemas.android.com/apk/res/android\"\n xmlns:app=\"http://schemas.android.com/apk/res-auto\"\n xmlns:tools=\"http://schemas.android.com/tools\"\n android:id=\"@+id/root\"\n android:layout_width=\"match_parent\"\n android:layout_height=\"52dp\"\n android:background=\"#e4e6e4\"\n android:padding=\"4dp\"\u003e\n\n \u003cImageView\n android:id=\"@+id/image\"\n android:layout_width=\"48dp\"\n android:layout_height=\"48dp\"\n android:background=\"#5c5c74\"\n android:contentDescription=\"An example box\"\n app:layout_constraintBottom_toBottomOf=\"parent\"\n app:layout_constraintStart_toStartOf=\"parent\"\n app:layout_constraintTop_toTopOf=\"parent\" /\u003e\n\n \u003cTextView\n android:id=\"@+id/title\"\n android:layout_width=\"0dp\"\n android:layout_height=\"0dp\"\n android:layout_marginStart=\"4dp\"\n android:background=\"#745c74\"\n app:layout_constraintBottom_toTopOf=\"@+id/subtitle\"\n app:layout_constraintEnd_toEndOf=\"parent\"\n app:layout_constraintStart_toEndOf=\"@id/image\"\n app:layout_constraintTop_toTopOf=\"parent\" /\u003e\n\n \u003cTextView\n android:id=\"@+id/subtitle\"\n android:layout_width=\"0dp\"\n android:layout_height=\"0dp\"\n android:background=\"#7e8d6e\"\n app:layout_constraintBottom_toBottomOf=\"parent\"\n app:layout_constraintEnd_toEndOf=\"parent\"\n app:layout_constraintStart_toStartOf=\"@id/title\"\n app:layout_constraintTop_toBottomOf=\"@+id/title\" /\u003e\n \u003c/androidx.constraintlayout.widget.ConstraintLayout\u003e\n \n```\n\nInspection of the new layout looks like this:\n**Figure 3.** Layout Inspector 3D mode.\n\nThe benefits of this are multiplied, because this layout is used for every\nitem in a list.\n\nMost of the difference is due to the use of `layout_weight` in the\n`LinearLayout` design, which can slow down measurement. This is one\nexample of how each layout has appropriate uses. Carefully consider whether\nusing layout weight is necessary.\n\nIn some complex layouts, the system might waste effort measuring the same UI\nelement more than once. This phenomenon is called *double taxation* . For\nmore information about double taxation and how to prevent it, see\n[Performance\nand view hierarchies](/topic/performance/rendering/optimizing-view-hierarchies).\n\nUse lint\n--------\n\nIt's good practice to run the [lint](/studio/write/lint) tool\non your layout files to search for possible view hierarchy optimizations. Lint\nreplaces the layoutopt tool and has greater functionality. The following are\nexamples of lint\n[rules](http://tools.android.com/tips/lint-checks):\n\n- Use compound drawables. You can handle a `LinearLayout` that contains an [ImageView](/reference/android/widget/ImageView) and a [TextView](/reference/android/widget/TextView) more efficiently as a compound drawable.\n- Merge root frame. If the root of a layout is a [FrameLayout](/reference/android/widget/FrameLayout) that doesn't provide background or padding, you can replace it with a merge tag, which is slightly more efficient.\n- Remove useless leaves. You can remove a layout that has no children or no background---since it's invisible---for a flatter and more efficient layout hierarchy.\n- Remove useless parents. You can remove a layout with a child that has no siblings, isn't a [ScrollView](/reference/android/widget/ScrollView) or a root layout, and doesn't have a background. You can also move the child view directly into the parent for a flatter and more efficient layout hierarchy.\n- Avoid deep layouts. Layouts with too much nesting are bad for performance. Consider using flatter layouts, such as [ConstraintLayout](/develop/ui/views/layout/constraint-layout), to improve performance. The default maximum depth for lint checks is 10.\n\nAnother benefit of the lint tool is its integration into Android Studio. Lint\nautomatically runs whenever you compile your program. With Android Studio, you\ncan also run lint inspections for a specific build variant or for all build\nvariants.\n\nYou can also manage inspection profiles and configure inspections within\nAndroid Studio with the **File \\\u003e Settings \\\u003e Project\nSettings** option. The Inspection Configuration page appears with the\nsupported inspections:\n**Figure 4.** Inspection Configuration page.\n\nLint can automatically fix some issues, provide suggestions for others, and\njump directly to the offending code for review.\n\nFor more information, see\n[Layouts](/guide/topics/ui/declaring-layout) and\n[Layout\nresource](/guide/topics/resources/layout-resource#include-element)."]]