0

Looks like that one way to avoid nested weights on Android's LinearLayout is to set a weight parameter to the nested LinearLayout. For instance, this layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<View
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1.0"
    android:background="#FF0000" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="2.0" >

    <View
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="10.0"
        android:background="#00FF00" />

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="20.0"
        android:orientation="vertical" >

        <View
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="100.0"
            android:background="#0000FF" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="200.0" >

            <View
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1000.0"
                android:background="#FF0000" />

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="2000.0"
                android:orientation="vertical" >

                <View
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:layout_weight="10000.0"
                    android:background="#00FF00" />

                <View
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:layout_weight="20000.0"
                    android:background="#0000FF" />
            </LinearLayout>
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

Produces this: enter image description here Although it is heavily nested and the nested weights are much bigger than the parent weights. Does the LinearLayout weight parameter really stops the weight propagation? This is true for all API levels?

EDIT

To be more clear: I want to use nested weights without performance loss and without using other layouts like RelativeLayout, I think that this is a solution, but I'm not so sure.

aalmeida
  • 339
  • 3
  • 5
  • Not sure that I understand the exact question. The nested weights will not prevent you from achieving the effect. It just warns you about using nested weights. – Jay Snayder Jan 27 '14 at 17:37
  • I want to stop the propagation of weights to nested/parents layouts, the real thing, not the lint warning. – aalmeida Jan 27 '14 at 17:40
  • 1
    Nested weight are bad for performance. See [this](http://stackoverflow.com/questions/9430764/why-are-nested-weights-bad-for-performance-alternatives) – Phantômaxx Jan 27 '14 at 17:48
  • @Tobor: I think that it's bad for performance only if it's propagated. In this example, looks like that the the first red view with weight 1.0 is NOT compared with the last blue view with weight 20000.0, so, if it's not propagated, it doesn't affects performance. – aalmeida Jan 27 '14 at 17:56

1 Answers1

0

Linear layout weight applies only to that particular layout's children. There's no weight value "propagation" - it's the actual size of children that matters.

Each weighted linear layout first measures the children as usual and then does addional measure/layout pass, assigning any remaining space in the linear layout to the children in proportion of their weights. If all weights are zero (as is the case in non-weigted layout), this step is not necessary.

Why nested weights are bad for perfomance is that each nesting level doubles the number of measure/layout passes. For example, in your example with 4 weighted linear layouts, there will be 2^4 = 16 measure/layout passes.

laalto
  • 150,114
  • 66
  • 286
  • 303