Why is 0dp considered a performance enhancement?
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"
.
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 :
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.