Android 程式開發:(五)屏幕組件 —— 5.4 TableLayout表格佈局

TableLayout可以把視圖views組織成“行”或“列”。可以使用<TableRow>元素指定表格中的一行。每一行又可以包含一個或多個視圖。每行中的每個視圖組成瞭表格的一個元素。每列的寬度,取決於這一列中寬度最大的視圖view。

觀察main.xml中的代碼:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent" >  
  5.   
  6.     <TableRow>  
  7.   
  8.         <TextView  
  9.             android:text="User Name:"  
  10.             android:width="120dp" />  
  11.   
  12.         <EditText  
  13.             android:id="@+id/txtUserName"  
  14.             android:width="200dp" />  
  15.     </TableRow>  
  16.   
  17.     <TableRow>  
  18.   
  19.         <TextView android:text="Password:" />  
  20.   
  21.         <EditText  
  22.             android:id="@+id/txtUserName"  
  23.             android:password="true" />  
  24.     </TableRow>  
  25.   
  26.     <TableRow>  
  27.   
  28.         <TextView />  
  29.   
  30.         <CheckBox  
  31.             android:id="@+id/chkRememberPassword"  
  32.             android:layout_width="fill_parent"  
  33.             android:layout_height="wrap_content"  
  34.             android:text="Remember Password" />  
  35.     </TableRow>  
  36.   
  37.     <TableRow>  
  38.   
  39.         <Button  
  40.             android:id="@+id/buttonSignIn"  
  41.             android:text="Log In" />  
  42.     </TableRow>  
  43.   
  44. </TableLayout>  

模擬器上的效果圖:

以上的例子,TableLayout中有2列,4行。在“Password” TextView視圖的正下方,是一個空的<TextView>元素。如果不這麼做的話,“Remember Password” CheckBox就會出現在“Password”TextView視圖的下面,就像這樣:

摘自 manoel的專欄

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *