How does setting baselineAligned to false improve performance in LinearLayout?

AndroidListviewAndroid Lint

Android Problem Overview


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?

Android Solutions


Solution 1 - Android

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)

Solution 2 - Android

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.

Solution 3 - Android

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionChristopher PerryView Question on Stackoverflow
Solution 1 - AndroidNima GView Answer on Stackoverflow
Solution 2 - AndroidZar E AhmerView Answer on Stackoverflow
Solution 3 - AndroidboiledwaterView Answer on Stackoverflow