Why is 0dp considered a performance enhancement?

ⅰ亾dé卋堺 提交于 2019-11-27 02:45:46
Lalit Poptani

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

Archie.bpgc

So when android:layout_weight is used on View X and LinearLayout is horizontal, then X's android:layout_width is simply ignored.

Similar, when android:layout_weight is used on View X and LinearLayout is vertical, then X's android:layout_height is ignored.

This actually means, that you can put anything in those ignored fields: 0dp or fill_parent or wrap_content. It doesn't matter. But it's recommended to use 0dp so View's do not do extra calculation of their height or width (which is then ignored). This small trick simply saves CPU cycles.

from :

What is the trick with 0dip layout_height or layouth_width?

as far as i know , there is a difference between using 0dp (or 0px btw, it's the same since 0 is 0 no matter what is the unit here ) and the wrap_content or fill_parent (or match_parent, it's the same).

it depends on the weight you use . if you only use weight of 1 , they all look the same , but the meaning is always different , and it is important for performance.

in order to show this , try the following:

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

  <TextView android:id="@+id/textView1" android:layout_width="match_parent"
    android:layout_height="0px" android:text="1" android:background="#ffff0000"
    android:layout_weight="1" android:gravity="center"
    android:textColor="#ffffffff" android:textSize="20sp" />

  <TextView android:id="@+id/textView2" android:layout_width="match_parent"
    android:layout_height="0px" android:text="2" android:background="#ff00ff00"
    android:layout_weight="2" android:gravity="center"
    android:textColor="#ffffffff" android:textSize="20sp" />

  <TextView android:id="@+id/textView3" android:layout_width="match_parent"
    android:layout_height="0px" android:text="3" android:background="#ff0000ff"
    android:layout_weight="3" android:gravity="center"
    android:textColor="#ffffffff" android:textSize="20sp" />

</LinearLayout>

and then try to replace the 0px with match_parent . you will see that the result is very different.

usually , for both better understanding and for better performance , you would want to use 0px.

LinearLayout measures all the children according to the layout_width/layout_height values, then divides up the leftover space (which may be negative) according to the layout_weight values.

0dp is more efficient than wrap_content in this case because it's more efficient to just use zero for the original height and then split the parent's full height based on the weight than to measure the child first and then split the remainder based on the weight.

So the efficiency comes from not measuring the child. 0dp should be exactly as efficient (and produce exactly the same result) as match_parent, or 42px, or any other fixed number.

Caution re using android:layout_height="0dp"

I have found that in a ListView (with the recommended View recycling using convertView, see e.g. http://lucasr.org/2012/04/05/performance-tips-for-androids-listview/), setting android:layout_height="0dp" for the row TextView can lead to text truncation for multi-line text content.

Whenever a TextView object that was previously used to display a text that fitted in a single line is recycled to display a longer text that needs more than one line, that text is truncated to a single line.

The problem is cured by using android:layout_height="wrap_content"

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!