Why are nested weights bad for performance? Alternatives?

后端 未结 6 1602
深忆病人
深忆病人 2020-11-22 16:26

I have written a couple layout files where I used the layout_weight attribute to create a ratio between different views.

At some point, I start getting

6条回答
  •  礼貌的吻别
    2020-11-22 16:58

    The main reason why nested weights are bad is that when a layout has children with a weight, it has to be measured twice (I think this is mentioned in the lint-warning). This means that a weighted layout that also contains a weighted layout has to measured four times, and each 'layer' of weights you add increase the measures with a power of two.

    In ICS (API level 14) the GridLayout was added, which allows simple and 'flat' solutions for many layouts which previously required weights. If you are developing for earlier versions of Android you will have a slightly harder time removing weights, but using a RelativeLayout and flattening as much as possible of your layout into that cab usually remove a lot of the nested weights.

提交回复
热议问题