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 ?
Setting it to 0px (or 0dip ) will skip the cpu cycles necessary for the initial initialization and the view's width/height will be adjust as required.
Equal distribution. To create a linear layout in which each child uses the same amount of space on the screen, set the android:layout_height of each view to "0dp" (for a vertical layout) or the android:layout_width of each view to "0dp" (for a horizontal layout).
This is heavily used for views withing LinearLayout
. There are three "layout" attributes that LinearLayout
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 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.
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:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:text="TextView" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:text="TextView" />
</LinearLayout>
In that case, the view will take as much height as all other views.
The android:layout_height="0dp"
is used in various codes because:
e.g:
android:layout_height = "0dp"
android:layout_weight = "1.0"
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With