Android常用佈局範例

在Android開發中UI設計十分重要,當用戶使用一個軟件時,最先感受到的不是這款軟件的功能是否強大,而是界面設計是否精緻,用戶體驗是否良好。也可以這樣說,有一個好的界面設計去吸引用戶的使用,才能讓更多的用戶體驗到軟件功能的強大。 下面着重講一下Android中幾種常用佈局的使用:

首先,需要說明的是,各個佈局既可以單獨使用,也可以嵌套使用,讀者在實際應用中應靈活掌握。

1 LinearLayout 是一種Android中最常用的佈局之一,它將自己包含的子元素按照一個方向排列。方向的設置通過Android:orientation=”vertical”(豎直)或者Android:orientation=”horizontal”(水平)來實現。


代碼如下:

  1.  <LinearLayout android:orientation="vertical" android:layout_width="fill_parent"  android:layout_height="fill_parent">     
  2.     <Button android:id="@+id/bt_1" android:layout_width="fill_parent"  
  3.     android:layout_height="wrap_content"  android:text="用來驗證RelativeLayout" 
  4. />
  5.     <Button android:id="@+id/bt_2" android:layout_width="fill_parent"  
  6.     android:layout_height="wrap_content" android:text="用來驗證TableLayout" 
  7.     /> 
  8.     <Button android:id="@+id/bt_3" android:layout_width="fill_parent"  
  9.     android:layout_height="wrap_content" android:text="用來驗證ListView" 
  10.     />   
  11.     <Button android:id="@+id/bt_4" android:layout_width="fill_parent"  
  12.     android:layout_height="wrap_content" android:text="用來驗證FrameLayout" 
  13.     />
  14. </LinearLayout> 


2 FrameLayout對象好比一塊在屏幕上提前預定好的空白區域,可以將一些元素填充在裏面,如圖片。所有元素都被放置在FrameLayout區域的最左上區域,而且無法爲這些元素制指定一個確切的位置,若有多個元素,那麼後面的元素會重疊顯示在前一個元素上。

代碼如下:

  1. <FrameLayout 
  2.   xmlns:android="http://schemas.android.com/apk/res/android" 
  3.   android:layout_width="match_parent" android:layout_height="match_parent"> 
  4.   <ImageView  android:id="@+id/photo" 
  5.        android:src="@drawable/img" android:layout_width="wrap_content" 
  6.        android:layout_height="wrap_content"/> 
  7. </FrameLayout> 


3 RelativeLayout是一種相對佈局,控件的位置是按照相對位置來計算的,後一個控件在什麼位置依賴於前一個控件的基本位置。是佈局最常用,也是最靈活的一種佈局。

 

 

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  2.     android:layout_width="match_parent" android:layout_height="match_parent" 
  3.     android:padding="10dip">     
  4.     <TextView android:id="@+id/label" android:layout_width="fill_parent" 
  5.         android:layout_height="wrap_content" android:text="請輸入用戶名"/> 
  6.     <EditText android:id="@+id/entry" android:layout_width="fill_parent" 
  7.          android:layout_height="wrap_content" android:layout_below="@id/label"/> 
  8.     <Button android:id="@+id/cancel" android:layout_width="wrap_content" 
  9.          android:layout_height="wrap_content" android:layout_below="@id/entry" 
  10.          android:text="取消"/>      
  11. </RelativeLayout> 


4 TableLayout
TableLayout是指將子元素的位置分配到行或列中。Android的一個TableLayout有許多TableRow組成,每一個TableRow都會定義一個Row。TableLayout容器不會顯示Row,Column,及Cell的邊框線,每個Row擁有0個或多個Cell,每個Cell擁有一個View對象。
在使用tablelayout時,應注意每一個cell的寬度。

 

 

  1. <TableLayout 
  2.   xmlns:android="http://schemas.android.com/apk/res/android"  
  3.   android:layout_width="match_parent" android:layout_height="match_parent">   
  4.   <TableRow> 
  5.     <TextView android:id="@+id/lable1"  android:text="用戶名" android:textStyle="bold" 
  6.         android:layout_width="55dip" android:gravity="center"/> 
  7.     <EditText android:id="@+id/entry1" android:layout_width="250dip" 
  8.         android:layout_height="wrap_content" 
  9.         /> 
  10.   </TableRow> 
  11.   <TableRow> 
  12.     <TextView android:id="@+id/lable2" android:textStyle="bold" android:text="密碼" 
  13.         android:layout_width="55dip" android:gravity="center"/> 
  14.     <EditText android:id="@+id/entry2" 
  15.         android:layout_width="250dip" android:layout_height="wrap_content" 
  16.         android:password="true" android:scrollHorizontally="true"/> 
  17.   </TableRow> 
  18. </TableLayout> 

 

本文出自 “HDDevTeam” 博客,請務必保留此出處http://hddev.blog.51cto.com/3365350/629635

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章