本文來自我的個人網站,如有興趣,歡迎訪問www.qingshuimonk.com!
1. 線性佈局(LinearLayout):在該標籤下的所有子元素會根據orientation屬性的值來決定是按行或者是按列來逐個顯示。代碼示例如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/app_name" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/test" />
</LinearLayout>
就會產生這樣的效果:
2. 另外還有相對佈局(RelativeLayout),比較簡單,這裏不再贅述。
3. 在實際中RelativeLayout和LinearLayout一般搭配使用,例如將剛纔的代碼改變一下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/test" />
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/button1"
android:layout_alignTop="@id/button1"
android:text="@string/hello_world" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="@string/app_name" />
</RelativeLayout>
</LinearLayout>
就有了這樣的效果:
4. 表格佈局(TableLayout)與html中的表格佈局類似,其中TableRow標籤代表一個行,而TextView標籤代表其中的一個元素。 表格佈局和幀佈局在初期用的較少,所以以後接觸到了再說。