Android UI教程 - Android LinearLayout
Android UI教程 - Android LinearLayout
Layout_weight和layout_gravity
在LinearLayout中,您可以将 layout_weight 和 layout_gravity 属性应用于视图包含在其中:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:layout_width="160dp"
android:layout_height="wrap_content"
android:text="Button"
android:layout_gravity="left"
android:layout_weight="1" />
<Button
android:layout_width="160dp"
android:layout_height="wrap_content"
android:text="Button"
android:layout_gravity="center"
android:layout_weight="2" />
<Button
android:layout_width="160dp"
android:layout_height="wrap_content"
android:text="Button"
android:layout_gravity="right"
android:layout_weight="3" />
</LinearLayout>
注意
layout_gravity 属性表示位置意见应该倾向。
layout_weight 属性指定可用空间的分布。
在前面的例子中,三个按钮占据约
- 16.6% (1/(1+2+3) * 100),
- 33.3% (2/(1+2+3) * 100), and
- 50% (3/(1+2+3) *100)
的可用高度。
水平LinearLayout
如果将 LinearLayout 的方向更改为 水平,你需要改变每个视图的宽度 到0 dp。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Button"
android:layout_gravity="left"
android:layout_weight="1" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Button"
android:layout_gravity="center_horizontal"
android:layout_weight="2" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Button"
android:layout_gravity="right"
android:layout_weight="3" />
</LinearLayout>