主用是用orientation屬性來把component(如button)做垂直及水平的排列,
另外,帶有權重(weight)的component將會填滿剩餘的空間
我們來舉個例子,來看看如何把component做垂直及水平的排列,及weight如何使用
1. LinearLayout – 水平(Horizontal)
打開res/layout/main.xml,加入 android:orientation="horizontal",然後增加3個按鈕,
在這個例子中,我們將權重設在button3,所以它將會填滿剩餘的空間
res/layout/main.xml
<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:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button1" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button2" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button3"
android:layout_weight="1"/>
</LinearLayout>
結果:2. LinearLayout – 垂直(Vertical)
我們把orientation改成“Vertical”
res/layout/main.xml
<?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" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button1" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button2" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button3"
android:layout_weight="1"/>
</LinearLayout>
結果:
沒有留言:
張貼留言