I mean why anybody want they view to be 0dip height ? I have seen this many times, there must be some kind of trick, but I do not get it.
<TextView android:gravity="top" android:textColor="#FFFF0000"
android:textSize="20dip" android:text="TextView"
android:layout_height="0dip" android:layout_width="fill_parent"
android:id="@+id/contactName"></TextView>
Why they don't use for example wrap_content ? what do they want to achieve ?
This is usually used when having many views inside a linearlayout and have set android:layout_weight="1" in order both views to take equal space. for example:
In that case, the view will take as much height as all other views.
This is heavily used for views withing
LinearLayout
. There are three "layout" attributes thatLinearLayout
is aware of:android:layout_height
android:layout_width
android:layout_weight
You can find example with
android:layout_weight
in tutorial project.So when
android:layout_weight
is used onView
X andLinearLayout
is horizontal, then X'sandroid:layout_width
is simply ignored.Similar, when
android:layout_weight
is used onView
X andLinearLayout
is vertical, then X'sandroid:layout_height
is ignored.This actually means, that you can put anything in those ignored fields:
0dp
orfill_parent
orwrap_content
. It doesn't matter. But it's recommended to use0dp
soView
's do not do extra calculation of their height or width (which is then ignored). This small trick simply saves CPU cycles.The
android:layout_height="0dp"
is used in various codes because:e.g:
Height or width when set to "0dp", are mostly used in combination with "weight". e.g. you want to fill all the available space for height then use the above code and like wise the same case for width.