TableLayout可以把視圖views組織成“行”或“列”。可以使用<TableRow>元素指定表格中的一行。每一行又可以包含一個或多個視圖。每行中的每個視圖組成瞭表格的一個元素。每列的寬度,取決於這一列中寬度最大的視圖view。
觀察main.xml中的代碼:
- <?xml version="1.0" encoding="utf-8"?>
- <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent" >
- <TableRow>
- <TextView
- android:text="User Name:"
- android:width="120dp" />
- <EditText
- android:id="@+id/txtUserName"
- android:width="200dp" />
- </TableRow>
- <TableRow>
- <TextView android:text="Password:" />
- <EditText
- android:id="@+id/txtUserName"
- android:password="true" />
- </TableRow>
- <TableRow>
- <TextView />
- <CheckBox
- android:id="@+id/chkRememberPassword"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="Remember Password" />
- </TableRow>
- <TableRow>
- <Button
- android:id="@+id/buttonSignIn"
- android:text="Log In" />
- </TableRow>
- </TableLayout>
模擬器上的效果圖:
以上的例子,TableLayout中有2列,4行。在“Password” TextView視圖的正下方,是一個空的<TextView>元素。如果不這麼做的話,“Remember Password” CheckBox就會出現在“Password”TextView視圖的下面,就像這樣:
摘自 manoel的專欄