在Android開發中UI設計十分重要,當用戶使用一個軟件時,最先感受到的不是這款軟件的功能是否強大,而是界面設計是否精緻,用戶體驗是否良好。也可以這樣說,有一個好的界面設計去吸引用戶的使用,才能讓更多的用戶體驗到軟件功能的強大。 下面着重講一下Android中幾種常用佈局的使用:
首先,需要說明的是,各個佈局既可以單獨使用,也可以嵌套使用,讀者在實際應用中應靈活掌握。
1 LinearLayout 是一種Android中最常用的佈局之一,它將自己包含的子元素按照一個方向排列。方向的設置通過Android:orientation=”vertical”(豎直)或者Android:orientation=”horizontal”(水平)來實現。
代碼如下:
- <LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent">
- <Button android:id="@+id/bt_1" android:layout_width="fill_parent"
- android:layout_height="wrap_content" android:text="用來驗證RelativeLayout"
- />
- <Button android:id="@+id/bt_2" android:layout_width="fill_parent"
- android:layout_height="wrap_content" android:text="用來驗證TableLayout"
- />
- <Button android:id="@+id/bt_3" android:layout_width="fill_parent"
- android:layout_height="wrap_content" android:text="用來驗證ListView"
- />
- <Button android:id="@+id/bt_4" android:layout_width="fill_parent"
- android:layout_height="wrap_content" android:text="用來驗證FrameLayout"
- />
- </LinearLayout>
2 FrameLayout對象好比一塊在屏幕上提前預定好的空白區域,可以將一些元素填充在裏面,如圖片。所有元素都被放置在FrameLayout區域的最左上區域,而且無法爲這些元素制指定一個確切的位置,若有多個元素,那麼後面的元素會重疊顯示在前一個元素上。
代碼如下:
- <FrameLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent" android:layout_height="match_parent">
- <ImageView android:id="@+id/photo"
- android:src="@drawable/img" android:layout_width="wrap_content"
- android:layout_height="wrap_content"/>
- </FrameLayout>
3 RelativeLayout是一種相對佈局,控件的位置是按照相對位置來計算的,後一個控件在什麼位置依賴於前一個控件的基本位置。是佈局最常用,也是最靈活的一種佈局。
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent" android:layout_height="match_parent"
- android:padding="10dip">
- <TextView android:id="@+id/label" android:layout_width="fill_parent"
- android:layout_height="wrap_content" android:text="請輸入用戶名"/>
- <EditText android:id="@+id/entry" android:layout_width="fill_parent"
- android:layout_height="wrap_content" android:layout_below="@id/label"/>
- <Button android:id="@+id/cancel" android:layout_width="wrap_content"
- android:layout_height="wrap_content" android:layout_below="@id/entry"
- android:text="取消"/>
- </RelativeLayout>
4 TableLayout
TableLayout是指將子元素的位置分配到行或列中。Android的一個TableLayout有許多TableRow組成,每一個TableRow都會定義一個Row。TableLayout容器不會顯示Row,Column,及Cell的邊框線,每個Row擁有0個或多個Cell,每個Cell擁有一個View對象。
在使用tablelayout時,應注意每一個cell的寬度。
- <TableLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent" android:layout_height="match_parent">
- <TableRow>
- <TextView android:id="@+id/lable1" android:text="用戶名" android:textStyle="bold"
- android:layout_width="55dip" android:gravity="center"/>
- <EditText android:id="@+id/entry1" android:layout_width="250dip"
- android:layout_height="wrap_content"
- />
- </TableRow>
- <TableRow>
- <TextView android:id="@+id/lable2" android:textStyle="bold" android:text="密碼"
- android:layout_width="55dip" android:gravity="center"/>
- <EditText android:id="@+id/entry2"
- android:layout_width="250dip" android:layout_height="wrap_content"
- android:password="true" android:scrollHorizontally="true"/>
- </TableRow>
- </TableLayout>
本文出自 “HDDevTeam” 博客,請務必保留此出處http://hddev.blog.51cto.com/3365350/629635