为什么 0dp 被认为是一种性能增强? [英] Why is 0dp considered a performance enhancement?

查看:36
本文介绍了为什么 0dp 被认为是一种性能增强?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

已填写本问题末尾的答案,结合评论和解决方案.

问题

我四处搜索,但没有找到任何能真正解释为什么 Android Lint 以及一些 Eclipse 提示建议替换一些 layout_heightlayout_width 值为 0dp.

例如,我有一个 ListView 建议更改

之前

</ListView>

之后

</ListView>

同样,它建议更改ListView 项.这些在更改前后看起来都一样,但我很想知道为什么这些是性能提升器.

有人解释一下为什么吗?如果有帮助,这里是 ListView 的总体布局.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent"><图像视图android:id="@+id/logo_splash"android:layout_width="match_parent"android:layout_height="wrap_content"></ImageView><线性布局android:layout_width="fill_parent"android:layout_height="fill_parent"机器人:方向=垂直"机器人:背景=@颜色/背景"android:layout_below="@id/logo_splash"><列表视图android:id="@android:id/列表"android:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1"></ListView><文本视图android:id="@android:id/空"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/no_upcoming"/></LinearLayout></RelativeLayout>

答案

我在这里提供答案是因为它实际上是答案和下面参考链接的组合.如果我做错了什么,请告诉我.

来自 0dip 有什么技巧layout_height 还是 layouth_width?

有 3 个通用布局属性适用于 widthheight

  1. android:layout_height
  2. android:layout_width
  3. android:layout_weight

LinearLayoutvertical时,layout_weight会影响子height视图s (ListView).将 layout_height 设置为 0dp 将导致该属性被忽略.

示例

<列表视图android:id="@android:id/列表"android:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1"></ListView></LinearLayout>

LinearLayout水平时,layout_weight会影响子width视图s (ListView).将 layout_width 设置为 0dp 将导致该属性被忽略.

示例

<列表视图android:id="@android:id/列表"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"></ListView></LinearLayout>

要忽略该属性的原因是,如果您不忽略它,它将用于计算占用更多 CPU 时间的布局.

此外,这可以防止在使用三个属性的组合时对布局的外观产生任何混淆.@android developer 在下面的回答中强调了这一点.

此外,Android LintEclipse 都说要使用 0dip.从下面的答案中,您可以使用 0dip0dp0px 等,因为任何单位中的零大小都是相同的.

避免在 ListView 上使用 wrap_content

来自ListView的Layout_width

如果你曾经想知道为什么 getView(...) 像我一样被调用了这么多次,结果证明它与 wrap_content 相关.>

像我上面使用的那样使用 wrap_content 会导致所有子 View 被测量,这将导致更多的 CPU 时间.此测量将导致您的 getView(...) 被调用.我现在已经对此进行了测试,并且 getView(...) 被调用的次数大大减少了.

当我在两个 ListView 上使用 wrap_content 时,getView(...) 在一个 ListView 和 4 次,用于另一行.

将其更改为推荐的 0dpgetView(...) 对每一行仅调用一次.这是一个相当大的改进,但与避免在 ListView 上的 wrap_content 相比,它与 0dp 的关系更大.

然而,0dp 的建议确实因此大大提高了性能.

解决方案

首先你有这个,

</ListView>

千万不要把ListView的高度当成wrap_content,那样会带来麻烦.这里 是原因,这个答案.

更多,

<块引用>

我四处搜索,但没有找到任何可以真正解释原因的内容Android Lint 以及一些 Eclipse 提示建议替换一些layout_height 和 layout_width 值为 0dp.

这是因为您使用的是 layout_weight = "1" 这意味着您的 ListView 会尽可能多地获取可用的高度.因此,在这种情况下,无需使用 layout_height = "wrap_content" 只需将其更改为 android:layout_height="0dp" 并且 ListView 的高度将由 layout_weight = "1".

An answer at the end of this question has been filled out, combining remarks and solutions.

Question

I searched around but haven't found anything that really explains why Android Lint as well as some Eclipse hints suggest replacing some layout_height and layout_width values with 0dp.

For example, I have a ListView that was suggested to be changed

Before

<ListView
    android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1">
</ListView>

After

<ListView
    android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1">
</ListView>

Similarly, it suggested changes to a ListView item. These all look the same before and after the changes, but I'm interested in understanding why these are performance boosters.

Anyone have an explanation of why? If it helps, here is general layout with the ListView.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <ImageView
        android:id="@+id/logo_splash"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    </ImageView>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:background="@color/background"
        android:layout_below="@id/logo_splash">

        <ListView
            android:id="@android:id/list"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1">
        </ListView>

        <TextView
            android:id="@android:id/empty"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/no_upcoming" />

    </LinearLayout>        
</RelativeLayout>

Answer

I'm putting in an answer here because it's really a combination of answers and referenced links below. If I'm wrong on something, do let me know.

From What is the trick with 0dip layout_height or layouth_width?

There are 3 general layout attributes that work with width and height

  1. android:layout_height
  2. android:layout_width
  3. android:layout_weight

When a LinearLayout is vertical, then the layout_weight will effect the height of the child Views (ListView). Setting the layout_height to 0dp will cause this attribute to be ignored.

Example

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">
    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">
    </ListView>
</LinearLayout>

When a LinearLayout is horizontal, then the layout_weight will effect the width of the child Views (ListView). Setting the layout_width to 0dp will cause this attribute to be ignored.

Example

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal">
    <ListView
        android:id="@android:id/list"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1">
    </ListView>
</LinearLayout>

The reason to want to ignore the attribute is that if you didn't ignore it, it would be used to calculate the layout which uses more CPU time.

Additionally this prevents any confusion over what the layout should look like when using a combination of the three attributes. This is highlighted by @android developer in an answer below.

Also, Android Lint and Eclipse both say to use 0dip. From that answer below, you can use 0dip, 0dp, 0px, etc since a zero size is the same in any of the units.

Avoid wrap_content on ListView

From Layout_width of a ListView

If you've ever wondered why getView(...) is called so many times like I have, it turns out to be related to wrap_content.

Using wrap_content like I was using above will cause all child Views to be measured which will cause further CPU time. This measurement will cause your getView(...) to be called. I've now tested this and the number of times getView(...) is called is reduced dramatically.

When I was using wrap_content on two ListViews, getView(...) was called 3 times for each row on one ListView and 4 times for each row on the other.

Changing this to the recommended 0dp, getView(...) was called only once for each row. This is quite an improvement, but has more to do with avoiding wrap_content on a ListView than it does the 0dp.

However the suggestion of 0dp does substantially improve performance because of this.

解决方案

First of all you have this,

<ListView
    android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1">
</ListView>

Never take the ListView's height as wrap_content, that will lead into troubles. Here is the reason for that and this answer.

Further more,

I searched around but haven't found anything that really explains why Android Lint as well as some Eclipse hints suggests replacing some layout_height and layout_width values with 0dp.

Its because you are using layout_weight = "1" that means your ListView with take the height as much as is available to it. So, in that case there is no need of using layout_height = "wrap_content" just change it to android:layout_height="0dp" and ListView's height will be managed by layout_weight = "1".

这篇关于为什么 0dp 被认为是一种性能增强?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆