在android布局中,layout_height =“0dip”的效果/含义是什么

我见过几个使用android:layout_height =“0px”或“0dip”的例子,但我不明白这个的影响。 看来,这将使布局0像素高。 价值是减轻了,但还有其他因素,如“重量”或任何父视图的高度?

是的,你是正确的重量,当你想要的宽度或高度由重量控制约定将该值设置为0dip,并让权重控制实际值。 虽然我很确定0在这里是任意的,但是你可以把0放到任何地方,这样可以使你的意图更加清晰。

如果将layout_height设置为非零值并将layout_height (或layout_width )设置为0px或0dip,则在使用LinearLayout时,LinearLayout将根据权重沿相应轴分配任何未分配的空间。 因此,例如,如果您查看具有id * gestures_overlay *的视图下方的布局,则它具有layout_height 0dip和layout_weight 1,因此,父LinearLayout将其拉伸以填充周围2个LinearLayout之间的可用垂直空间。 如果有另一个视图具有相同的0dip layout_heightlayout_weight值,那么他们将根据它们的权重值共享垂直空间。

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

来自官方开发人员文档的相关示例( http://developer.android.com/guide/topics/ui/layout/linear.html ):

同等重量的孩子

要创build一个线性布局,其中每个孩子在屏幕上使用相同的空间量,请将每个视图的android:layout_height设置为“ 0dp ”(对于垂直布局)或将每个视图的android:layout_width设置为“ 0dp ”(为水平布局)。 然后将每个视图的android:layout_weight设置为“1”。