Вложенные веса против вложенных макетов с весами. Что лучше?

Android Lint предупреждает нас, когда мы делаем вложенные веса, говоря, что вложенные веса плохо влияют на производительность, поэтому мне было интересно, что произойдет, если я добавлю дополнительную раскладку, включающую вложенные схемы взвешивания. Будет ли это иметь какое-то значение в производительности?

Парень здесь сказал, что веса макета требуют, чтобы виджет был измерен дважды. Когда LinearLayout с ненулевыми весами вложен в другой LinearLayout с ненулевыми весами, то количество измерений увеличивается в геометрической прогрессии.

Так что мне было интересно

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

Лучше чем

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

1 ответ

Решение

Второй слой макетов не препятствует возникновению / распространению дополнительной обработки.

Я понимаю, что на самом деле linearlayout ВСЕГДА выполняет два прохода, но это может быть просто тем, что происходит на практике, потому что, если вы не используете весовые коэффициенты, вы можете сделать тот же самый макет, используя относительное расположение.

Просто чтобы уточнить. Распространение onMeasure происходит дважды, когда корень движется вниз. В обоих случаях размеры кадра измеряются 4 раза.

Другие вопросы по тегам