tablelayout是一种Android UI布局,继承自linearLayout, 一行中添加单个组件:直接在TableLayout中添加组件 一行中添加多个组件:使用TableRow,将组件写在TableRow下 TableLayout 中有多少个组件,就有多少行 (一个tablerow一行,一个单独的组件也一行!多少列则是看tableRow中 的组件个数,组件最多的就是TableLayout的列数)
常用属性: tableLayout下 android:collapseColumns:设置需要被隐藏的列的序号 android:shrinkColumns:设置允许被收缩的列的列序号 android:stretchColumns:设置运行被拉伸的列的列序号(可沾满剩余空间) android:layout_column="2":表示的就是跳过第二个,直接显示到第三个格子处,从1开始算的! android:layout_span="4":表示合并4个单元格,也就说这个组件占4个单元格 TableRow的子控件的主要属性: android:layout_column=”1” 该控件显示在第1列 android:layout_span=”2” 该控件占据2
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent">
<TableLayout android:layout_width="match_parent" android:layout_height="100dp" android:background="@color/pink"> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="啥都没做"/> <TableRow android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="1-1"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="1-2"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="1-3"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="1-4"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="1-5"/> </TableRow> </TableLayout> <TableLayout android:background="@color/hui" android:layout_width="match_parent" android:layout_height="100dp" android:collapseColumns="2"> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="属性1:android:collapseColumns:需要被隐藏的列序号"/> <TableRow android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="1-1"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="1-2"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="1-3"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="1-4"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="1-5"/> </TableRow> </TableLayout>
<TableLayout android:background="@color/blue" android:layout_width="match_parent" android:layout_height="100dp" android:shrinkColumns="2"> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="属性2:android:shrinkColumns:允许被收缩的列序号"/> <TableRow android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="1-1"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="1-2"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="1-3"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="1-4"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="1-5"/> </TableRow> </TableLayout>
<TableLayout android:background="@color/green" android:layout_width="match_parent" android:layout_height="100dp" android:stretchColumns="1"> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="属性3:android:stretchColumns:允许被拉伸的列序号"/> <TableRow android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="1-1"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="1-2"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="1-3"/> </TableRow> </TableLayout>
<TableLayout android:background="@color/yellow" android:layout_width="match_parent" android:layout_height="300dp"> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="啥都没做"/> <TableRow android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="1-1"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="1-2"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="1-3"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="1-4"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="1-5"/> </TableRow> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="子控件属性:android:layout_column=”1”控件显示在第1列"/> <TableRow android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="2-1"/> <Button android:layout_column="3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="2-2"/> <Button
android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="2-3"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="2-4"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="2-5"/> </TableRow> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="子控件属性:android:layout_span=”2” 该控件占据2列"/> <TableRow android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="3-1"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="3-2"/> <Button android:layout_span="2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="3-3"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="3-4"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="3-5"/> </TableRow> <TableRow android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="4-1"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="4-2"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="4-3"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="4-4"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="4-5"/> </TableRow> </TableLayout> </LinearLayout>
