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

时间:2012-02-24 12:26:47

标签: android android-layout performance

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

在某些时候,我开始收到有关嵌套权重的lint警告。

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

谢谢!

6 个答案:

答案 0 :(得分:135)

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

  

布局权重需要测量窗口小部件两次。当一个   具有非零权重的LinearLayout嵌套在另一个内   LinearLayout具有非零权重,然后是测量次数   以指数方式增长。

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

答案 1 :(得分:61)

更新:正如我们所知,支持库百分比已从API级别26弃用。ConstraintLayout是实现相同平面xml结构的新方法。

Updated Github Project

更新了样本

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

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

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

Demo HERE

考虑这个简单的布局来演示相同的内容。

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的重量。真的很棒!!!。

答案 2 :(得分:43)

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

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

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

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

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

答案 3 :(得分:10)

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

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

答案 4 :(得分:3)

有一个简单的解决方案来避免嵌套的LinearLayouts带有权重-只需将TableLayoutout与weightSum一起使用,以及嵌套的LinearLayout和weightSum-Tablelayout与LinearLayout具有相同的属性(orientation,weightSum,layout_weight等),并且不显示消息- “嵌套的权重不利于性能”

示例:

 <TableLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:weightSum="1">

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="0.8"/>


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="0.2"
            android:orientation="horizontal"
            android:weightSum="1">


            <ImageView
                android:layout_height="match_parent"
                android:layout_width="0dp"
                android:layout_weight="0.4"/>

            <TextView
                android:layout_height="match_parent"
                android:layout_width="0dp"
                android:layout_weight="0.6"/>


            </LinearLayout>

    </TableLayout>

答案 5 :(得分:0)

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