将基线对齐设置为 false 如何提高 LinearLayout 的性能?

发布于 2025-01-06 23:10:01 字数 302 浏览 0 评论 0原文

我刚刚在 xml 中构建一些 UI,Lint 给了我一个警告,并说将 android:baselineAligned 设置为 false 以提高 ListView 的性能。

添加此警告的 Lint 更改的文档说

布局性能:在您应该的位置找到具有权重的 LinearLayouts 设置 android:baselineAligned="false" 以获得更好的性能,并且 查找可能会影响性能的嵌套权重的情况 问题。

有人可以解释为什么这可以提高表现,特别是在涉及重量时?

I was just building some UI in xml, and Lint gave me a warning and said to set android:baselineAligned to false to improve performance in ListView.

The docs for the Lint changes that added this warning say

Layout performance: Finds LinearLayouts with weights where you should
set android:baselineAligned="false" for better performance, and also
finds cases where you have nested weights which can cause performance
problems.

Can somebody explain why this improves performance, specifically when weight is involved?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

桃气十足 2025-01-13 23:10:01

通过设置 android:baselineAligned="false" ,您可以防止应用布局为了对齐其子级基线而必须执行的额外工作;这可以明显提高性能。 (减少UI上不必要的操作=>更好的性能)

By setting android:baselineAligned="false" , you're preventing the extra work your app's layout has to do in order to Align its children's baselines; which can obviously increase the performance. (Fewer unnecessary operations on UI => Better performance)

奢欲 2025-01-13 23:10:01

android:baselineAligned="false" 如何提供帮助。它可能不是答案,但有助于获得概念。

我刚刚设法让 3 个项目(图标、文本、按钮)居中
垂直于水平 LinearLayout。

这可能看起来很简单,但实际上指定
android:gravity="center_vertical" 作为 LinearLayout 属性不是
足够了 - 图标居中,但文本和按钮不居中。这是
因为(大概)文本有基线和居中算法
使用它代替“真正的”垂直中心。但更糟糕的是 - 按钮
(位于文本旁边)使用文本的基线居中!

在 LinearLayout 中指定 android:baselineAligned="false" 会变成这样
关闭,一切都正确居中。

how android:baselineAligned="false" help . It may not be the answer but help to get concept.

I've just managed to get 3 items (icon, text, button) centered
vertically in horizontal LinearLayout.

This may seem simple, but in reality specifying
android:gravity="center_vertical" as LinearLayout attribute is not
enough - icon is centered, but text and button are not. This is
because (presumably) text have a baseline, and centering algorithm
uses it instead of 'real' vertical center. But what is worse - button
(which comes next to text) is centered using text's baseline!

Specifying android:baselineAligned="false" in LinearLayout turns this
off, and everything centers correctly.

只是一片海 2025-01-13 23:10:01
// Baseline alignment requires to measure widgets to obtain the
                // baseline offset (in particular for TextViews). The following
                // defeats the optimization mentioned above. Allow the child to
                // use as much space as it wants because we can shrink things
                // later (and re-measure).
                if (baselineAligned) {
                    final int freeSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
                    child.measure(freeSpec, freeSpec);
                }

https://github.com/android /platform_frameworks_base/blob/master/core/java/android/widget/LinearLayout.java#L1093

// Baseline alignment requires to measure widgets to obtain the
                // baseline offset (in particular for TextViews). The following
                // defeats the optimization mentioned above. Allow the child to
                // use as much space as it wants because we can shrink things
                // later (and re-measure).
                if (baselineAligned) {
                    final int freeSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
                    child.measure(freeSpec, freeSpec);
                }

https://github.com/android/platform_frameworks_base/blob/master/core/java/android/widget/LinearLayout.java#L1093

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文