app性能优化:优化布局层次结构

张开发
2026/5/31 7:52:30 15 分钟阅读
app性能优化:优化布局层次结构
使用基本的布局结构会导致 最高效的布局不过您添加到应用中的每个微件和布局 需要进行初始化、布局和绘制。例如使用嵌套 实例 LinearLayout 会导致视图层次结构过深。此外将多个 使用 layout_weight 的 LinearLayout 实例 参数的开销可能特别大因为每个子参数都需要测量两次。 在反复膨胀布局时这尤为重要例如 在 RecyclerView。本文档介绍了如何使用 布局检查器和 lint 来检查和优化布局。检查布局Android SDK 工具包含 布局检查器工具 您可以在应用运行时分析布局使用此工具有助于您 并发现布局性能中的低效问题借助布局检查器您可以选择已连接的设备上正在运行的进程或 模拟器然后显示布局树。每个街区的红绿灯 表示其测量、布局和绘制性能这有助于您确定 潜在问题例如图 1 显示了用作 RecyclerView。此布局在左侧显示了一个小位图图像 右侧是两个堆叠的文本项。尤其需要注意的是 像这样多次膨胀的布局已经过优化因为 成倍提升性能。一张图片显示列表中的单项内容一张图片以及两段垂直对齐的文本图 1. 中某项的概念性布局 RecyclerView。布局检查器会显示可用设备及其正在运行的设备的列表 组件。从 Windows 标签页中选择您的组件然后点击 布局检查器用于查看所选组件的布局层次结构。 例如图 2 显示了由图所示的列表项的布局 1.显示布局检查器和 LinearLayout 组合的图片图 2. 中布局的布局层次结构 图 1使用嵌套的 LinearLayout 实例。修改布局由于前面的布局性能因嵌套 LinearLayout那么您可以将 也就是让布局变浅变宽 狭窄而深度。答 ConstraintLayout 因为根节点支持此类布局。当您将此设计转换为使用 ConstraintLayout 时布局将成为两层层次结构androidx.constraintlayout.widget.ConstraintLayout xmlns:androidhttp://schemas.android.com/apk/res/androidxmlns:apphttp://schemas.android.com/apk/res-autoxmlns:toolshttp://schemas.android.com/toolsandroid:idid/rootandroid:layout_widthmatch_parentandroid:layout_height52dpandroid:background#e4e6e4android:padding4dpImageView android:idid/imageandroid:layout_width48dpandroid:layout_height48dpandroid:background#5c5c74android:contentDescriptionAn example boxapp:layout_constraintBottom_toBottomOfparentapp:layout_constraintStart_toStartOfparentapp:layout_constraintTop_toTopOfparent/TextView android:idid/titleandroid:layout_width0dpandroid:layout_height0dpandroid:layout_marginStart4dpandroid:background#745c74app:layout_constraintBottom_toTopOfid/subtitleapp:layout_constraintEnd_toEndOfparentapp:layout_constraintStart_toEndOfid/imageapp:layout_constraintTop_toTopOfparent/TextView android:idid/subtitleandroid:layout_width0dpandroid:layout_height0dpandroid:background#7e8d6eapp:layout_constraintBottom_toBottomOfparentapp:layout_constraintEnd_toEndOfparentapp:layout_constraintStart_toStartOfid/titleapp:layout_constraintTop_toBottomOfid/title//androidx.constraintlayout.widget.ConstraintLayout检查新布局如下所示显示 3D 布局检查器的图片图 3. 布局检查器的 3D 模式。这样的好处会成倍增加因为这种布局用于 。大部分差异都是由于在layout_weight LinearLayout 设计这可能会导致衡量速度变慢。这个 展示如何恰当地使用每种布局的示例。请仔细考虑 需要使用布局权重。在一些复杂的布局中系统可能会浪费时间和精力测量相同的界面 多个元素。这种现象称为 Double Taxation。对于 如需详细了解 Double Taxation 以及如何防止出现这种情况请参阅 性能 和视图层次结构。使用 lint最好运行 lint 工具 以搜索可能的视图层次结构优化。lint 替换了 layoutopt 工具并具有更强大的功能。以下是 lint 示例 rules使用复合可绘制对象。您可以处理一个 LinearLayout 包含 ImageView 和 TextView 作为复合可绘制对象效率更高。合并根帧。如果布局的根是 FrameLayout 既没有提供背景或内边距则可将其替换为 合并标签效率稍高一些。去除无用的叶子。您可以移除没有任何子项的布局也可以 因为我们看不到任何背景所以更扁平、 高效的布局层次结构请移除没用的父级。您可以移除包含符合以下条件的子项的布局 兄弟姐妹不是 ScrollView 或根布局并且没有背景您还可以将 使子视图直接呈现在父项中使视图更扁平、更高效 布局层次结构。避免使用深层布局。嵌套过多的布局对 性能考虑使用较扁平的布局例如 ConstraintLayout, 以提高性能。lint 检查的默认最大深度为 10。lint 工具的另一个优势是它可以集成到 Android Studio 中。lint 会在您编译程序时自动运行。借助 Android Studio 还可以针对特定 build 变体或所有 build 运行 lint 检查 变体。您还可以在以下集群中管理检查配置文件和配置检查 在 Android Studio 中打开 File 设置 项目 设置选项。系统会显示“Inspection Configuration”页面其中包含 支持的检查一张图片显示 Android Studio 的“Inspections”菜单图 4. “Inspection Configuration”页面。Lint 可自动修复某些问题、为其他问题提供建议以及 直接跳转到违规代码进行审核。更多fw实战开发干货请关注下面“千里马学框架”https://mp.weixin.qq.com/s/IluAN5L7uyObBh0KopI7rg

更多文章