为什么0dp认为是性能增强? [英] Why is 0dp considered a performance enhancement?
问题描述
在答案是在这个问题年底已填写,结合意见和解决方案。
An answer at the end of this question has been filled out, combining remarks and solutions.
我四处寻找,但没有发现任何真正解释为什么的 Android的皮棉的以及部分的的Eclipse 的提示建议更换部分 layout_height
和 layout_width
与 0dp
。
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
.
例如,我有一个的ListView
的建议进行更改
For example, I have a ListView
that was suggested to be changed
之前
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
</ListView>
之后
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
</ListView>
同样,它建议更改为的 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.
任何人有任何的原因作出解释?如果有帮助,这里是用 ListView的总体布局
。
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.
从<一个href=\"http://stackoverflow.com/questions/7220404/what-is-the-trick-with-0dip-layout-height-or-layouth-width\">What与0dip layout_height的伎俩或layouth_width?
有3总体布局的属性与工作的宽度的和的高度的
There are 3 general layout attributes that work with width and height
-
的android:layout_height
-
的android:layout_width
-
的android:layout_weight
android:layout_height
android:layout_width
android:layout_weight
在的LinearLayout
是垂直的,那么 layout_weight
将影响的高度孩子的EM> 查看
S(的ListView
)。设置 layout_height
到 0dp
将导致此属性被忽略。
When a LinearLayout
is vertical, then the layout_weight
will effect the height of the child View
s (ListView
). Setting the layout_height
to 0dp
will cause this attribute to be ignored.
示例
<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>
在的LinearLayout
是横向的,那么 layout_weight
将影响的宽度的子查看
的S(的ListView
)。设置 layout_width
到 0dp
将导致此属性被忽略。
When a LinearLayout
is horizontal, then the layout_weight
will effect the width of the child View
s (ListView
). Setting the layout_width
to 0dp
will cause this attribute to be ignored.
示例
<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>
想忽略属性的原因是,如果你没有忽视它,它会被用来计算它使用更多的CPU时间的布局。
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.
此外该prevents在布局应该是什么用三个属性的组合时,就像任何混乱。这是通过的 @android开发商的下一个答案突出显示。
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.
此外, Android的皮棉和的Eclipse 的都表示,使用 0dip
。从下面这个问题的答案,你可以使用 0dip
, 0dp
, 0像素
等由于零大小是任何单位的相同。
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.
避免在ListView控件WRAP_CONTENT
从<一个href=\"http://stackoverflow.com/questions/4270278/layout-width-of-a-listview/4270471#4270471\">Layout_width一个ListView 的
如果你曾经想知道为什么 getView(...)
叫了这么多次像我有,原来是相关的 WRAP_CONTENT
。
If you've ever wondered why getView(...)
is called so many times like I have, it turns out to be related to wrap_content
.
使用 WRAP_CONTENT
像我用上面会引起所有子查看
s到被测量,这将导致进一步的CPU时间。这种测量将导致您的 getView(...)
被调用。现在我已经试用了 getView(...)
叫的次数大大减少。
Using wrap_content
like I was using above will cause all child View
s 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.
当我使用 WRAP_CONTENT
在两个的ListView
S, getView(... )
是在一个的ListView
4次呼吁每行3倍于其他各行。
When I was using wrap_content
on two ListView
s, getView(...)
was called 3 times for each row on one ListView
and 4 times for each row on the other.
0dp , getView(...)将其更改为推荐的
被称为只有一次的每一行。这是相当的改善,但更多的是与避免 WRAP_CONTENT
在的ListView
比它的 0dp
。
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
.
但的0dp建议
并大幅度提高,因为这样的表现。
However the suggestion of 0dp
does substantially improve performance because of this.
推荐答案
你有这样的:首先,
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
</ListView>
从来不ListView的高度WRAP_CONTENT,这将导致进入的烦恼。 在这里
是其中的原因和的 这个答案
进一步,
我四处寻找,但没有发现任何真正解释了为什么
Android的皮棉以及一些Eclipse的提示建议更换部分
layout_height和layout_width值与0dp。
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.
它,因为你正在使用 layout_weight =1
这意味着采取高度尽可能提供给您它的ListView。所以,在这种情况下没有必要使用 layout_height =WRAP_CONTENT的
只是将其更改为的android:layout_height =0dp
和ListView的高度将通过 layout_weight =管理1
。
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屋!