Android-TableLayout布局

来源:互联网 发布:照片加相框软件 编辑:程序博客网 时间:2024/05/17 06:54
TableLayout,表格布局采用行列形式管理UI组件,TableLayout不需要明确地声明有多少行和列,而是通过添加TableRow、其它组件来控制表格的行数、列数。
v每次向TableLayout添加一个TableRow,就是在向表格添加一行,TableRow也是容器,可以向TableRow中添加组件,每添加一个组件,即是添加一列。
v如果直接向TableLayout添加组件,则认为这个组件占用一行。
v表格布局中列的宽度即是每一列中最宽的组件的宽度。
v表格布局中,可以为单元格设置如下三种属性:

 

XML属性
相关方法
说明
android:collapseColumns
setColumnCollapsed(int,boolean)
设置需要隐藏的列的序号,多个之间用逗号分隔
Android:shrinkColumns
setShrinkAllColumns(boolean)
设置允许被收缩的列的序号,多个之间用逗号分隔
Android:stretchColumns
setStretchAllColumns(boolean)
设置允许被拉伸的列的序号,多个之间用逗号分隔

xml布局文件:

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  3.     android:orientation="vertical" 
  4.     android:layout_width="fill_parent" 
  5.     android:layout_height="fill_parent" 
  6.     > 
  7.         <!-- 定义第一个表格布局,指定第2列允许收缩,第3列允许拉伸 --> 
  8.         <TableLayout 
  9.             android:layout_width="fill_parent"   
  10.             android:layout_height="wrap_content" 
  11.             android:shrinkColumns="1" 
  12.             android:stretchColumns="2" 
  13.         > 
  14.                 <!-- 直接添加按钮,它自己会占一行 --> 
  15.                 <Button 
  16.                     android:layout_width="wrap_content" 
  17.                     android:layout_height="wrap_content" 
  18.                     android:text="独自一行的按钮" 
  19.                     /> 
  20.                 <!-- 添加一个表格行 --> 
  21.                 <TableRow> 
  22.                 <!-- 为该表格行添加3个按钮 --> 
  23.                 <Button 
  24.                     android:layout_width="wrap_content" 
  25.                     android:layout_height="wrap_content" 
  26.                     android:text="普通按钮" 
  27.                     />    
  28.                 <Button 
  29.                     android:layout_width="wrap_content" 
  30.                     android:layout_height="wrap_content" 
  31.                     android:text="允许被收缩的按钮" 
  32.                     />   
  33.                 <Button 
  34.                     android:layout_width="wrap_content" 
  35.                     android:layout_height="wrap_content" 
  36.                     android:text="允许被拉伸的按钮" 
  37.                     /> 
  38.                 </TableRow>   
  39.         </TableLayout> 
  40.         <!-- 定义第二个表格布局 ,指定第二列隐藏--> 
  41.         <TableLayout 
  42.             android:layout_width="fill_parent"   
  43.             android:layout_height="wrap_content" 
  44.             android:collapseColumns="1" 
  45.         > 
  46.                 <!-- 直接添加按钮,它自己会占一行 --> 
  47.                 <Button 
  48.                     android:layout_width="wrap_content" 
  49.                     android:layout_height="wrap_content" 
  50.                     android:text=" 独自一行的按钮 " 
  51.                     /> 
  52.                 <!--定义一个表格行--> 
  53.                 <TableRow> 
  54.                 <!-- 为该表格行添加3个按钮 --> 
  55.                 <Button 
  56.                     android:layout_width="wrap_content" 
  57.                     android:layout_height="wrap_content" 
  58.                     android:text="普通按钮" 
  59.                     />    
  60.                 <Button 
  61.                     android:layout_width="wrap_content" 
  62.                     android:layout_height="wrap_content" 
  63.                     android:text="被隐藏的按钮" 
  64.                     />   
  65.                 <Button   
  66.                     android:layout_width="wrap_content" 
  67.                     android:layout_height="wrap_content" 
  68.                     android:text="普通按钮 " 
  69.                     /> 
  70.                 </TableRow>   
  71.         </TableLayout> 
  72.         <!-- 定义第三个表格布局 ,指定第2、3两列可以被拉伸--> 
  73.         <TableLayout 
  74.             android:layout_width="fill_parent"   
  75.             android:layout_height="wrap_content" 
  76.             android:stretchColumns="1,2" 
  77.         > 
  78.                 <!-- 直接添加按钮,它自己会占一行 --> 
  79.                 <Button 
  80.                     android:layout_width="wrap_content" 
  81.                     android:layout_height="wrap_content" 
  82.                     android:text="独自一行的按钮" 
  83.                     /> 
  84.                 <!--定义一个表格行--> 
  85.                 <TableRow> 
  86.                 <!-- 为该表格行添加3个按钮 --> 
  87.                 <Button 
  88.                     android:layout_width="wrap_content" 
  89.                     android:layout_height="wrap_content" 
  90.                     android:text="普通按钮" 
  91.                     />    
  92.                 <Button 
  93.                     android:layout_width="wrap_content" 
  94.                     android:layout_height="wrap_content" 
  95.                     android:text="允许被拉伸的按钮" 
  96.                     />   
  97.                 <Button 
  98.                     android:layout_width="wrap_content" 
  99.                     android:layout_height="wrap_content" 
  100.                     android:text="允许被拉伸的按钮" 
  101.                     /> 
  102.                 </TableRow>   
  103.                 <!--定义一个表格行--> 
  104.                 <TableRow> 
  105.                 <!-- 为该表格行添加2个按钮 --> 
  106.                 <Button 
  107.                     android:layout_width="wrap_content" 
  108.                     android:layout_height="wrap_content" 
  109.                     android:text="普通按钮" 
  110.                     />    
  111.                 <Button 
  112.                     android:layout_width="wrap_content" 
  113.                     android:layout_height="wrap_content" 
  114.                     android:text="允许被拉伸的按钮" 
  115.                     /> 
  116.                 </TableRow>   
  117.         </TableLayout> 
  118. </LinearLayout> 

效果图: 

 

 

原创粉丝点击