Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In android layouts what is the effect/meaning of layout_height="0dip"

I've seen several examples that use android:layout_height="0px" or "0dip" but i do not understand the impact of this. It seems that would make the layout 0 pixels tall. Is the value mitigated but some other factor like 'weight' or the height of any parent views?

like image 520
NewAndroidGuy Avatar asked May 13 '11 02:05

NewAndroidGuy


People also ask

What is 0dip?

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.

What does 0dp mean in Android?

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


2 Answers

Yep you are right about the weight, when you want the width or height to be controlled by weight its convention to set that value to 0dip and let the weight control the actual value. Although I am pretty sure 0 is just arbitrary here you could put anything but putting 0 makes your intention more clear.

like image 84
Nathan Schwermann Avatar answered Sep 19 '22 05:09

Nathan Schwermann


When using a LinearLayout if you set the layout_weight to a non-zero value and set the layout_height (or layout_width) to 0px or 0dip then the LinearLayout distributes any unassigned space along the appropriate axis based on the weights. So for example, if you look at the layout below the View with id *gestures_overlay* it has layout_height 0dip and layout_weight 1 so the parent LinearLayout stretches it to fill the available vertical space between the 2 surrounding LinearLayouts. If there was another View with the same 0dip layout_height and a layout_weight value then they would share the vertical space based on their weight values.

<?xml version="1.0" encoding="utf-8"?> <LinearLayout     xmlns:android="http://schemas.android.com/apk/res/android"      android:layout_width="fill_parent"     android:layout_height="fill_parent"      android:orientation="vertical">      <LinearLayout         android:layout_width="fill_parent"         android:layout_height="wrap_content"          android:orientation="horizontal">          <TextView             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:layout_marginRight="6dip"              android:text="@string/prompt_gesture_name"             android:textAppearance="?android:attr/textAppearanceMedium" />          <EditText             android:id="@+id/gesture_name"             android:layout_width="0dip"             android:layout_weight="1.0"             android:layout_height="wrap_content"              android:maxLength="40"             android:singleLine="true" />      </LinearLayout>      <android.gesture.GestureOverlayView         android:id="@+id/gestures_overlay"         android:layout_width="fill_parent"         android:layout_height="0dip"         android:layout_weight="1.0"          android:gestureStrokeType="multiple" />      <LinearLayout         style="@android:style/ButtonBar"          android:layout_width="fill_parent"         android:layout_height="wrap_content"          android:orientation="horizontal">          <Button             android:id="@+id/done"              android:layout_width="0dip"             android:layout_height="wrap_content"             android:layout_weight="1"              android:enabled="false"              android:onClick="addGesture"             android:text="@string/button_done" />          <Button             android:layout_width="0dip"             android:layout_height="wrap_content"             android:layout_weight="1"              android:onClick="cancelGesture"             android:text="@string/button_discard" />      </LinearLayout>  </LinearLayout> 
like image 29
Femi Avatar answered Sep 20 '22 05:09

Femi