首页 文章

为什么嵌套权重不利于性能?备择方案?

提问于
浏览
146

我编写了几个布局文件,我使用 layout_weight 属性创建不同视图之间的比率 .

在某些时候,我开始获得有关嵌套权重的lint警告 .

所以,我想知道为什么嵌套权重不利于性能,如果有一种更有效的方法来创建视图维度之间的恒定比率,可以用于不同的屏幕尺寸,并且不需要指定很多维度dpi值通过几个布局文件(我的意思是不同的屏幕尺寸) .

谢谢!

5 回答

  • 10

    嵌套权重对性能不利,因为:

    布局权重需要对窗口小部件进行两次测量 . 当具有非零权重的LinearLayout嵌套在具有非零权重的另一个LinearLayout内时,则测量数量呈指数增长 .

    最好使用RelativeLayout并根据其他视图的位置调整视图,而不使用特定的dpi值 .

  • 40

    Update: 我们知道支持库百分比已从API级别26弃用. ConstraintLayout 是实现相同平面xml结构的新方法 .

    Updated Github Project

    Updated Samples:

    <android.support.constraint.ConstraintLayout 
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <TextView
            android:id="@+id/fifty_thirty"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:background="#ffff8800"
            android:gravity="center"
            android:text="@string/fifty_fifty_text"
            android:textColor="@android:color/white"
            app:layout_constraintHeight_default="percent"
            app:layout_constraintHeight_percent="0.5"
            android:textSize="25sp"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintWidth_default="percent"
            app:layout_constraintWidth_percent="0.5" />
    
        <TextView
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:background="#ffff5566"
            android:gravity="center"
            android:text="@string/fifty_fifty_text"
            android:textColor="@android:color/white"
            android:textSize="25sp"
            app:layout_constraintHeight_default="percent"
            app:layout_constraintHeight_percent="0.5"
            app:layout_constraintLeft_toRightOf="@id/fifty_thirty"
            app:layout_constraintTop_toBottomOf="@id/fifty_thirty"
            app:layout_constraintWidth_default="percent"
            app:layout_constraintWidth_percent="0.5" />
    
    </android.support.constraint.ConstraintLayout>
    

    Update: 好消息android百分比支持库解决了我们的性能问题和嵌套凌乱的加权 LinearLayout

    compile 'com.android.support:percent:23.0.0'
    

    在这里演示

    考虑这个简单的布局来演示相同的 .

    percent support libray demo

    <android.support.percent.PercentRelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <TextView
            android:id="@+id/fifty_huntv"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:background="#ff7acfff"
            android:text="20% - 50%"
            android:textColor="@android:color/white"
            app:layout_heightPercent="20%"
            app:layout_widthPercent="50%" />
        <TextView
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_toRightOf="@id/fifty_huntv"
            android:background="#ffff5566"
            android:text="80%-50%"
            app:layout_heightPercent="80%"
            app:layout_widthPercent="50%"
            />
    
    </android.support.percent.PercentRelativeLayout>
    

    避免性能降低嵌套 LinearLayout 与weights.Really真棒!

  • 54

    我想(而且我可能会因此受到抨击),但我认为我的手机还有一个四核处理器可以与大多数人家用PC竞争(如果不是完全毁掉的话) .

    我也认为这种硬件功能是手机的未来 .

    所以我得出一个结论,只要你没有被嵌套(在MHO中,布局应该永远不会超过4级,如果是你可能做错了),你的手机可能会少关心关于有重量 .

    您可以做很多事情会对性能产生更深远的影响,然后担心您的处理器会做一些额外的数学运算 .

    (请注意,我有点幽默,所以不要在这篇文章中过于认真,除此之外,还有其他事情你应该先优化的想法,而担心2-3级深度的重量并没有帮助你的 Health )

  • 0

    嵌套权重不好的主要原因是,当布局中有子权重时,必须测量两次(我认为这在lint-warning中提到) . 这意味着包含加权布局的加权布局必须测量四次,并且您添加的每个“层”权重都会增加两次幂的度量 .

    在ICS(API级别14)中添加了GridLayout,这允许简单和'flat'解决方案用于以前需要权重的许多布局 . 如果您正在为早期版本的Android开发,那么删除权重会稍微困难一些,但使用 RelativeLayout 并尽可能地将布局展平到该cab中通常会删除大量嵌套权重 .

  • 132

    我认为,唯一的选择是创建一个名为onResume的函数,并设置所有大小和位置 . 无论如何,按重量你可以只设置尺寸但没有填充(所以布局变得更复杂),没有textSize(不可能以某种方式补偿),更不用说诸如线条之类的东西了 .

相关问题