public class TableLayout extends LinearLayout
查过文档,整理下要点:
我们无法为 TableLayout 的 children 指定 layout_width 属性。宽度永远是 MATCH_PARENT。但 layout_height 属性可以由 child 定义平;默认值是 WRAP_CONTENT。如果一个 child 是 TableRow,则高度总是 WRAP_CONTENT。
表的单元必须按照增加列的排序填至行中,在代码和XML文件都要这样。列的索引是从0开始的。如果你不为 child 单元指定列序号,其会自动向下一个有效列增加。如果你跳过某列的索引,这一列会被视为这一行的空单元。
虽然 TableLayout 典型的 children 是 TableRow,但你也可用任何 View 的子类来作为其直接的 child(第一点)。这个 View 会作为一个占据所有列的单行显示。
看了 APIDemos 的一些例子,感觉比较全面了,这个布局比较简单,也不是很常用,主要是一些规则要熟悉熟悉,下面就直接从 ApiDemos 选一些典型的例子,基本够用了。
本文地址:http://www.cnblogs.com/rossoneri/p/4369333.html
资源文件:
1 <string name="table_layout_1_star">*</string> 2 <string name="table_layout_1_triple_star">***</string> 3 <string name="table_layout_1_open">Open\u2026</string> 4 <string name="table_layout_1_open_shortcut">Ctrl-O</string> 5 <string name="table_layout_1_save">Save As\u2026</string> 6 <string name="table_layout_1_save_shortcut">(Save Document)</string> 7 <string name="table_layout_1_quit">Quit Application</string> 8 <string name="table_layout_1_quit_shortcut">Ctrl-Shift-Q</string> 9 <string name="table_layout_2_path_1">~/path/to/file/to/open</string> 10 <string name="table_layout_2_path_2">~/.profile</string> 11 <string name="table_layout_2_open">Open</string> 12 <string name="table_layout_2_save">Save</string> 13 <string name="table_layout_2_save_all">Save All</string> 14 <string name="table_layout_3_star">*</string> 15 <string name="table_layout_3_triple_star">***</string> 16 <string name="table_layout_3_open">Open\u2026</string> 17 <string name="table_layout_3_open_shortcut">Ctrl-O</string> 18 <string name="table_layout_3_save">Save As\u2026</string> 19 <string name="table_layout_3_save_shortcut">(Save Document)</string> 20 <string name="table_layout_3_too_long">Too Long</string> 21 <string name="table_layout_3_quit">Quit Application</string> 22 <string name="table_layout_3_quit_shortcut">Ctrl-Shift-Q</string> 23 <string name="table_layout_4_open">Open\u2026</string> 24 <string name="table_layout_4_open_shortcut">Ctrl-O</string> 25 <string name="table_layout_4_save">Save As\u2026</string> 26 <string name="table_layout_4_save_shortcut">Ctrl-Shift-S</string> 27 <string name="table_layout_5_open">Open\u2026</string> 28 <string name="table_layout_5_open_shortcut">Ctrl-O</string> 29 <string name="table_layout_5_save">Save\u2026</string> 30 <string name="table_layout_5_save_shortcut">Ctrl-S</string> 31 <string name="table_layout_5_save_as">Save As\u2026</string> 32 <string name="table_layout_5_save_as_shortcut">Ctrl-Shift-S</string> 33 <string name="table_layout_5_import">Import\u2026</string> 34 <string name="table_layout_5_export">Export\u2026</string> 35 <string name="table_layout_5_export_shortcut">Ctrl-E</string> 36 <string name="table_layout_5_quit">Quit\u2026</string> 37 <string name="table_layout_6_x">X</string> 38 <string name="table_layout_6_open">Open\u2026</string> 39 <string name="table_layout_6_open_shortcut">Ctrl-O</string> 40 <string name="table_layout_6_save">Save\u2026</string> 41 <string name="table_layout_6_save_shortcut">Ctrl-S</string> 42 <string name="table_layout_6_save_as">Save As\u2026</string> 43 <string name="table_layout_6_save_as_shortcut">Ctrl-Shift-S</string> 44 <string name="table_layout_6_import">Import\u2026</string> 45 <string name="table_layout_6_export">Export\u2026</string> 46 <string name="table_layout_6_export_shortcut">Ctrl-E</string> 47 <string name="table_layout_6_quit">Quit\u2026</string> 48 <string name="table_layout_7_x">X</string> 49 <string name="table_layout_7_open">Open\u2026</string> 50 <string name="table_layout_7_open_shortcut">Ctrl-O</string> 51 <string name="table_layout_7_save">Save\u2026</string> 52 <string name="table_layout_7_save_shortcut">Ctrl-S</string> 53 <string name="table_layout_7_save_as">Save As\u2026</string> 54 <string name="table_layout_7_save_as_shortcut">Ctrl-Shift-S</string> 55 <string name="table_layout_7_import">Import\u2026</string> 56 <string name="table_layout_7_export">Export\u2026</string> 57 <string name="table_layout_7_export_shortcut">Ctrl-E</string> 58 <string name="table_layout_7_toggle_checkmarks">Toggle Checkmarks</string> 59 <string name="table_layout_7_toggle_shortcuts">Toggle Shortcuts</string> 60 <string name="table_layout_8_x">X</string> 61 <string name="table_layout_8_open">Open\u2026</string> 62 <string name="table_layout_8_open_shortcut">Ctrl-O</string> 63 <string name="table_layout_8_save">Save\u2026</string> 64 <string name="table_layout_8_save_shortcut">Ctrl-S</string> 65 <string name="table_layout_8_save_as">Save As\u2026</string> 66 <string name="table_layout_8_save_as_shortcut">Ctrl-Shift-S</string> 67 <string name="table_layout_8_import">Import\u2026</string> 68 <string name="table_layout_8_export">Export\u2026</string> 69 <string name="table_layout_8_export_shortcut">Ctrl-E</string> 70 <string name="table_layout_8_toggle_stretch">Toggle Stretch</string> 71 <string name="table_layout_9_open">Open\u2026</string> 72 <string name="table_layout_9_open_shortcut">Ctrl-O</string> 73 <string name="table_layout_9_save">Save\u2026</string> 74 <string name="table_layout_9_save_shortcut">Ctrl-S</string> 75 <string name="table_layout_9_save_as">Save As\u2026</string> 76 <string name="table_layout_9_save_as_shortcut">Ctrl-Shift-S</string> 77 <string name="table_layout_9_save_all">Save All And Do A Lot Of Stuff Just To Be Too Long For This Screen Because It Is A Test After All</string> 78 <string name="table_layout_9_save_all_shortcut">Ctrl-E</string> 79 <string name="table_layout_9_import">Import\u2026</string> 80 <string name="table_layout_9_export">Export\u2026</string> 81 <string name="table_layout_9_export_shortcut">Ctrl-E</string> 82 <string name="table_layout_9_toggle_shrink">Toggle Shrink</string> 83 <string name="table_layout_10_user">User</string> 84 <string name="table_layout_10_password">Password</string> 85 <string name="table_layout_10_cancel">Cancel</string> 86 <string name="table_layout_10_login">Login</string> 87 <string name="table_layout_12_a">A</string> 88 <string name="table_layout_12_b">BB</string> 89 <string name="table_layout_12_c">CCCC</string> 90 <string name="table_layout_12_d">D</string> 91 <string name="table_layout_12_e">E</string> 92 <string name="table_layout_12_f">F</string> 93 <string name="table_layout_12_g">G</string> 94 <string name="table_layout_12_h">H</string>
1.基本用法:
1 <?xml version="1.0" encoding="utf-8"?> 2 <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent"> 5 6 <TableRow> 7 <TextView 8 android:text="@string/table_layout_1_star" 9 android:padding="3dip" /> 10 <TextView 11 android:text="@string/table_layout_1_open" 12 android:padding="3dip" /> 13 <TextView 14 android:text="@string/table_layout_1_open_shortcut" 15 android:padding="3dip" /> 16 </TableRow> 17 18 <TableRow> 19 <TextView 20 android:text="@string/table_layout_1_triple_star" 21 android:padding="3dip" /> 22 <TextView 23 android:text="@string/table_layout_1_save" 24 android:padding="3dip" /> 25 <TextView 26 android:text="@string/table_layout_1_save_shortcut" 27 android:padding="3dip" /> 28 </TableRow> 29 30 <TableRow> 31 <TextView 32 android:text="@string/table_layout_1_star" 33 android:padding="3dip" /> 34 <TextView 35 android:text="@string/table_layout_1_quit" 36 android:padding="3dip" /> 37 <TextView 38 android:text="@string/table_layout_1_quit_shortcut" 39 android:padding="3dip" /> 40 </TableRow> 41 </TableLayout>
效果:
2.有空单元的情况:
1 <TableLayout 2 android:layout_width="match_parent" 3 android:layout_height="match_parent"> 4 5 <TableRow> 6 <Button 7 android:text="@string/table_layout_2_open" /> 8 <TextView 9 android:text="@string/table_layout_2_path_1" 10 android:padding="3dip" /> 11 </TableRow> 12 <TableRow> 13 <Button 14 android:text="@string/table_layout_2_save_all"/> 15 </TableRow> 16 <TableRow> 17 <Button 18 android:text="@string/table_layout_2_save" 19 android:visibility="invisible" /> 20 <TextView 21 android:text="@string/table_layout_2_path_2" 22 android:padding="3dip" /> 23 </TableRow> 24 </TableLayout>
效果
3.加上对齐属性: stretchColumn="1" 拉伸第一列
1 <?xml version="1.0" encoding="utf-8"?> 2 <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:stretchColumns="1"> 6 7 <TableRow> 8 <TextView 9 android:text="@string/table_layout_4_open" 10 android:padding="3dip" /> 11 <TextView 12 android:text="@string/table_layout_4_open_shortcut" 13 android:gravity="right" 14 android:padding="3dip" /> 15 </TableRow> 16 17 <TableRow> 18 <TextView 19 android:text="@string/table_layout_4_save" 20 android:padding="3dip" /> 21 <TextView 22 android:text="@string/table_layout_4_save_shortcut" 23 android:gravity="right" 24 android:padding="3dip" /> 25 </TableRow> 26 </TableLayout>
效果:
把stretchColumns="1"改为 stretchColumns="0,1",两列都拉伸
效果:
4.span
1 <TableLayout 2 android:layout_width="match_parent" 3 android:layout_height="wrap_content"> 4 5 <TableRow> 6 <TextView 7 android:text="@string/table_layout_12_a" 8 android:background="#FFFF0000" 9 android:padding="3dip" /> 10 <TextView 11 android:text="@string/table_layout_12_b" 12 android:background="#FF00FF00" 13 android:padding="3dip" /> 14 <TextView 15 android:text="@string/table_layout_12_c" 16 android:background="#FF0000FF" 17 android:padding="3dip" /> 18 </TableRow> 19 20 <TableRow> 21 <TextView 22 android:text="@string/table_layout_12_d" 23 android:layout_span="2" 24 android:gravity="center_horizontal" 25 android:background="#FF0000FF" 26 android:padding="3dip" /> 27 <TextView 28 android:text="@string/table_layout_12_e" 29 android:background="#FF00FF00" 30 android:padding="3dip" /> 31 </TableRow> 32 33 <TableRow> 34 <TextView 35 android:text="@string/table_layout_12_f" 36 android:background="#FFFF00FF" 37 android:padding="3dip" /> 38 <TextView 39 android:text="@string/table_layout_12_g" 40 android:background="#FF00FF00" 41 android:padding="3dip" /> 42 <TextView 43 android:text="@string/table_layout_12_h" 44 android:background="#FFFF0000" 45 android:padding="3dip" /> 46 </TableRow> 47 48 <TableRow> 49 <TextView 50 android:text="@string/table_layout_12_a" 51 android:background="#FF00FF00" 52 android:padding="3dip" /> 53 <TextView 54 android:text="@string/table_layout_12_b" 55 android:layout_span="2" 56 android:gravity="center_horizontal" 57 android:background="#FF0000FF" 58 android:padding="3dip" /> 59 </TableRow> 60 61 <TableRow> 62 <TextView 63 android:text="@string/table_layout_12_g" 64 android:layout_span="3" 65 android:gravity="center_horizontal" 66 android:background="#FFC0C0C0" 67 android:padding="3dip" /> 68 </TableRow> 69 </TableLayout>
效果:
5.复杂布局,garvity 和 layout_gravity 对齐,前者是其 child 的位置,后者是其自身的位置:
1 <TableLayout 2 android:layout_width="match_parent" 3 android:layout_height="wrap_content" 4 android:stretchColumns="1"> 5 6 <TableRow> 7 <TextView 8 android:layout_column="1" 9 android:text="@string/table_layout_7_open" 10 android:padding="3dip" /> 11 <TextView 12 android:text="@string/table_layout_7_open_shortcut" 13 android:gravity="right" 14 android:padding="3dip" /> 15 </TableRow> 16 17 <TableRow> 18 <TextView 19 android:layout_column="1" 20 android:text="@string/table_layout_7_save" 21 android:background="#FF00FF00" 22 android:padding="3dip" /> 23 <TextView 24 android:text="@string/table_layout_7_save_shortcut" 25 android:gravity="right" 26 android:padding="3dip" /> 27 </TableRow> 28 29 <TableRow> 30 <!-- Horizontally centers the content of the cell --> 31 <TextView 32 android:layout_column="1" 33 android:text="@string/table_layout_7_save_as" 34 android:background="#FFFF0000" 35 android:layout_gravity="center_horizontal" 36 android:padding="3dip" /> 37 <TextView 38 android:text="@string/table_layout_7_save_as_shortcut" 39 android:background="#FFFF00FF" 40 android:gravity="right" 41 android:padding="3dip" /> 42 </TableRow> 43 44 <View 45 android:layout_height="2dip" 46 android:background="#FF909090" /> 47 48 <TableRow> 49 <TextView 50 android:text="@string/table_layout_7_x" 51 android:padding="3dip" /> 52 <TextView 53 android:text="@string/table_layout_7_import" 54 android:padding="3dip" /> 55 </TableRow> 56 57 <TableRow> 58 <View 59 android:layout_height="68dip" 60 android:background="#FF909090" /> 61 <!-- Aligns the content of the cell to the bottom right --> 62 <TextView 63 android:text="@string/table_layout_7_export" 64 android:background="#FFFF0000" 65 android:layout_gravity="right|bottom" 66 android:padding="3dip" /> 67 <TextView 68 android:text="@string/table_layout_7_export_shortcut" 69 android:background="#FF00FFFF" 70 android:gravity="right" 71 android:padding="3dip" /> 72 </TableRow> 73 74 <View 75 android:layout_height="2dip" 76 android:background="#FF909090" /> 77 </TableLayout>
效果