表格佈局與常見的彪哥相似,以行、列的形式來管理放入其中的UI組件。表格佈局使用<TableLayout>標記定義,在表格佈局中,能夠添加多個<TableRow>標記,每一個<TableRow>標記佔用一行。因爲<TableRow>標記也是容器,全部還可在該標記找那個添加其餘組件,每沒加一個組件,表格就會增長一列。在XML中表格佈局的基本語法格式以下:android
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 屬性列表> <TableRow 屬性列表>須要添加的UI組件</TableRow> 多個<TableRow> </TableLayout>
TableLayout繼承了LinearLayout,所以它徹底支持LinearLayout所支持的所有XML屬性。佈局
同時還支持以下所示的XML屬性spa
XML屬性 | 描述 |
android:collapseColumns | 設置須要被隱藏列的列序號(序號從0開始),多個列序號之間用逗號「,」分割。 |
android:shrinkColumns | 設置容許被收縮列的列序號(序號從0開始),多個列序號之間用逗號「,」分割。 |
android:stretchColumns | 設置容許被拉伸列的列序號(序號從0開始),多個列序號之間用逗號「,」分割。 |
android:layout_column | 該單元格在第幾列顯示 |
android:layout_span | 該單元格佔據列數,默認爲1 |
<?xml version="1.0" encoding="utf-8"?> <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:stretchColumns="0,3"> <TableRow android:id="@+id/tablerow1" android:layout_weight="wrap_content" android:layout_height="wrap_content"> <TextView /> <TextView android:id="@+id/textview1" android:layout_weight="wrap_content" android:layout_height="wrap_content" android:text="用戶名:" android:textSize="24px"> <EditText android:id="@+id/edittext1" android:layout_weight="wrap_content" android:layout_height="wrap_content" android:hint="請輸入賬號" android:textSize="24px" android:minWidth="400px"/> <TextView /> </TableRow> <TableRow android:id="@+id/tablerow2" android:layout_weight="wrap_content" android:layout_height="wrap_content"> <TextView /> <TextView android:id="@+id/textview2" android:layout_weight="wrap_content" android:layout_height="wrap_content" android:text="密 碼:" android:textSize="24px"/> <EditText android:id="@+id/edittext2" android:layout_weight="wrap_content" android:layout_height="wrap_content" android:hint="請輸入密碼" android:textSize="24px" android:minWidth="400px"/> <TextView /> </TableRow> <TableRow android:id="@+id/tablerow3" android:layout_weight="wrap_content" android:layout_height="wrap_content"> <TextView /> <Button android:id="@+id/button1" android:layout_weight="wrap_content" android:layout_height="wrap_content" android:text="登陸"/> <Button android:id="@+id/button2" android:layout_weight="wrap_content" android:layout_height="wrap_content" android:text="退出"/> <TextView /> </TableRow> </TableLayout>
效果圖以下:.net