简体   繁体   English

嵌套权重与带有权重的嵌套布局。 哪个更好?

[英]Nested Weights Vs Nested Layouts with weights. Which is better?

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. 当我们执行嵌套权重时,Android Lint会警告我们说嵌套权重不利于性能,因此我想知道如果我添加一个额外的布局来封装嵌套权重布局会怎样。 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. 当权重为非零的LinearLayout嵌套在权重为非零的另一个LinearLayout内时,度量的数量将呈指数增长。

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. 我的理解是,linearlayout实际上总是执行两次遍历,但这实际上可能就是实际发生的事情,因为如果您不使用权重,则可以使用relativelayout进行相同的布局。

Just to elaborate. 只是为了详细说明。 the onMeasure propogation occurs twice from the root moving down. 从根向下移动时,onMeasure传播发生两次。 In both cases, the framelayouts are measured 4 times. 在这两种情况下,均会测量帧布局4次。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM