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