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

Lint文档中添加了这个警告

布局性能:找到线性布局的权重,你应该 设置android: baselininealigned ="false",以获得更好的性能 找到嵌套权重可能导致性能下降的情况 问题。

有人能解释一下为什么这样能提高成绩吗,特别是当涉及到体重的时候?


通过设置android:baselineAligned="false",你防止你的应用程序的布局必须做的额外工作,以对齐它的孩子的基线;这样可以明显提高性能。(减少UI上不必要的操作=>性能更好)


// 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


how android: baselinelinaligned ="false"帮助。这可能不是答案,但有助于获得概念。

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.