简体   繁体   English

Android布局出现错误“嵌套的权重不利于性能”

[英]Android layout with error “Nested weights are bad for perfomance”

im trying to do this layout above 我试图在上面做这个布局 在此处输入图片说明

and here is my code 这是我的代码

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="0.80"
        android:orientation="horizontal" >

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="0.50"
            android:padding="10dp" />

        <RelativeLayout>
        </RelativeLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="0.50"
            android:orientation="vertical" >
        </LinearLayout>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="0.20"
        android:orientation="vertical" >
    </LinearLayout>

</LinearLayout>

In ImageView, i got the error "Nested weights are bad for perfomance". 在ImageView中,出现错误“嵌套的权重不利于性能”。 I would to understand why this happening and if someone have a better solution for this layout. 我想知道为什么会这样,如果有人对此布局有更好的解决方案。

The layout_weight attribute is another way of specifying a width or a height in percentage dimensions, and to optimize the calculation of the dimensions you should use 0dp to one of them (the one you are replacing with). layout_weight属性是在百分比尺寸中指定宽度或高度的另一种方法,为了优化尺寸的计算,应在其中一个(您要替换的尺寸)中使用0dp

Another optimize tip is to remove the <RelativeLayout></RelativeLayout> that is not being used in your layout. 另一个优化技巧是删除布局中未使用的<RelativeLayout></RelativeLayout>

Please try the solution below: 请尝试以下解决方案:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.80"
        android:orientation="horizontal" >

        <ImageView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="0.50"
            android:padding="10dp" />

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="0.50"
            android:orientation="vertical" >
        </LinearLayout>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.20"
        android:orientation="vertical" >
    </LinearLayout>

</LinearLayout>

The error "Nested weights are bad for performance" happens because you have a weighted LInearLayout "nested" inside another weighted LinearLayout (see below). 发生错误“嵌套的权重不利于性能”是因为在另一个加权的LinearLayout中有一个加权的LInearLayout“嵌套”(请参见下文)。 In order for Android to perform this it will have to draw the entire screen to get all of the non-nested layout measurements, then draw it all again to calculate the "nested" weighted layouts. 为了让Android执行此操作,必须绘制整个屏幕以获取所有非嵌套的布局尺寸,然后再次全部绘制以计算“嵌套”的加权布局。

This can be a huge performance hit. 这可能会严重影响性能。 However, I built one screen that had 4 layers of nesting and about 100 layouts, including lots of images. 但是,我构建了一个具有4层嵌套和大约100种布局的屏幕,其中包括许多图像。 Rendering took about 5 milliseconds on newer devices, usually less. 在较新的设备上渲染大约需要5毫秒,通常更少。 So this is really only an issue if you are frequently redrawing the nested layouts (like trying to animation with nested layouts). 因此,如果您经常重绘嵌套的版式(例如尝试使用嵌套的版式制作动画),这实际上只是一个问题。 If you don't see a performance hit that you can measure in your logcat or observe while using your app, don't worry about it. 如果您看不到可以在logcat中衡量或在使用应用程序时观察到的性能下降,请不必担心。 Test thoroughly! 彻底测试!

In other words, it's a warning, not an error. 换句话说,这是警告,而不是错误。

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="0.80"
    android:orientation="horizontal" >

    <ImageView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="0.50"
        android:padding="10dp" />

    <!-- THIS IS YOUR NESTED WEIGHTED LAYOUT -->
    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="0.50"
        android:orientation="vertical" >
    </LinearLayout>
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="0.20"
    android:orientation="vertical" >
</LinearLayout>

I would rather write it as below. 我宁愿如下。 Notice using integers instead of fractions: 注意使用整数而不是分数:

    <?xml version="1.0" encoding="utf-8"?>

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

    <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="4"
            android:orientation="horizontal" >

        <ImageView
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:padding="10dp" />

        <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:orientation="vertical" >
        </LinearLayout>
    </LinearLayout>

    <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:orientation="vertical" >
    </LinearLayout>

</LinearLayout> 

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

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