Вопрос

Android Lint gives us warning when we do nested weights saying that nested weights are bad for performance so i was wondering what will happen if i add an extra layout which encloses the nested weigh layouts. Will it make any difference in performance?

The guy here said that Layout weights require a widget to be measured twice. When a LinearLayout with non-zero weights is nested inside another LinearLayout with non-zero weights, then the number of measurements increase exponentially.

So i was wondering is

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_weight="1" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:baselineAligned="false"
            android:gravity="center"
            android:orientation="horizontal" >

            <FrameLayout
                android:id="@+id/fl_topleft"
                android:layout_width="0dip"
                android:layout_height="match_parent"
                android:layout_margin="5dp"
                android:layout_weight="1"
                android:background="@drawable/shape" />

            <FrameLayout
                android:id="@+id/fl_topright"
                android:layout_width="0dip"
                android:layout_height="match_parent"
                android:layout_margin="5dp"
                android:layout_weight="1"
                android:background="@drawable/shape" />
        </LinearLayout>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_weight="1" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:baselineAligned="false"
            android:gravity="center"
            android:orientation="horizontal" >

            <FrameLayout
                android:id="@+id/fl_bottomleft"
                android:layout_width="0dip"
                android:layout_height="match_parent"
                android:layout_margin="5dp"
                android:layout_weight="1"
                android:background="@drawable/shape" />

            <FrameLayout
                android:id="@+id/fl_bottomright"
                android:layout_width="0dip"
                android:layout_height="match_parent"
                android:layout_margin="5dp"
                android:layout_weight="1"
                android:background="@drawable/shape" />
        </LinearLayout>
    </LinearLayout>

</LinearLayout>

Better than

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dip"
         android:orientation="horizontal"
        android:layout_weight="1" >



            <FrameLayout
                android:id="@+id/fl_topleft"
                android:layout_width="0dip"
                android:layout_height="match_parent"
                android:layout_margin="5dp"
                android:layout_weight="1"
                android:background="@drawable/shape" />

            <FrameLayout
                android:id="@+id/fl_topright"
                android:layout_width="0dip"
                android:layout_height="match_parent"
                android:layout_margin="5dp"
                android:layout_weight="1"
                android:background="@drawable/shape" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dip"
         android:orientation="horizontal"
        android:layout_weight="1" >



            <FrameLayout
                android:id="@+id/fl_bottomleft"
                android:layout_width="0dip"
                android:layout_height="match_parent"
                android:layout_margin="5dp"
                android:layout_weight="1"
                android:background="@drawable/shape" />

            <FrameLayout
                android:id="@+id/fl_bottomright"
                android:layout_width="0dip"
                android:layout_height="match_parent"
                android:layout_margin="5dp"
                android:layout_weight="1"
                android:background="@drawable/shape" />

    </LinearLayout>

</LinearLayout>
Это было полезно?

Решение

The second layer of layouts doesn't prevent the extra processing from occurring/propagating.

My understanding is that linearlayout actually ALWAYS does the two passes but that could be simply what happens in practice because if you aren't using weights, you could do the same layout using relativelayout.

Just to elaborate. the onMeasure propogation occurs twice from the root moving down. In both cases, the framelayouts are measured 4 times.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top