Android五种布局管理器之『TableLayout』

表格布局(TableLayout)类以行和列的形式管理控件,每一行为一个TableRow对象,也可以作为一个View对象;当为View对象时,该View对象将跨越该行的所有列。在TableRow中可以添加子控件,每添加一个子控件即为一列。

TableLayout布局中并不会为每一行、每一列或每个单元格绘制边框,每一行可以有0个或多个单元格,每个单元格为一个View对象。TableLayout中可以有空的单元格,单元格也可以像HTML中那样跨越多个列。

在TableLayout布局中,一个列的宽度由该列中最宽的那个单元格指定,而表格的宽度是由父容器指定的。在TableLayout中,可以为列设置三种属性:

  • Shrinkable:如果一个列被标识为Shrinkable,则该列的宽度可以进行收缩,以使表格能够适应其父容器的大小。
  • Stretchable:如果一个列被标识为Stretchable,则该列的宽度可以进行拉伸,以使填满表格中的空闲空间。
  • Collapsed:如果一个列被标识为Collapsed,则该列会被隐藏。

注意:一个列可以同时具有Shrinkable属性和Stretchable属性,在这种情况下,该列的宽度将任意拉伸或收缩以适应父容器。

TableLayout继承自LinearLayout类,除了继承来自父类的属性和方法,TableLayout类中还包含表格布局所特有的属性和方法,如下表:

属性名称 对应方法 描述
android:collapseColumnssetColumnCollapsed(int,boolean)设置指定列号的列属性为Collapsed
android:shrinkColumnssetShrinkAllColumns(boolean)设置指定列号的列属性为Shrinkable
android:stretchColumnssetStretchAllColumns(boolean)设置指定列号的列属性为Stretchable

注意:TableLayout中所谓的列序号是从0开始计算的。setShrinkAllColumns和setStretchAllColumns实现的功能是将表格中的所有列设置为Shrinkable或Stretchable。

下面来看一下效果:

点击放大图片

其中Main.xml代码如下:

 
 
 
 
  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <LinearLayout  
  3.    android:id="@+id/LinearLayout01"  
  4.    android:layout_width="fill_parent"  
  5.    android:layout_height="fill_parent"  
  6.    xmlns:android="http://schemas.android.com/apk/res/android" 
  7.    android:orientation="vertical" 
  8.    android:background="@drawable/android" 
  9.    android:gravity="bottom"> 
  10.     
  11.    <!-- 第一行 --> 
  12.    <TableLayout  
  13.    android:id="@+id/TableLayout01"  
  14.    android:layout_width="fill_parent"  
  15.    android:layout_height="wrap_content"    
  16.    android:background="#FFFFFF" 
  17.    xmlns:android="http://schemas.android.com/apk/res/android"> 
  18.        <TextView  
  19.          android:text="我是单独的一行"  
  20.          android:id="@+id/TextView01"  
  21.          android:layout_width="wrap_content"  
  22.          android:layout_height="wrap_content" 
  23.          android:layout_centerInParent="true" 
  24.          android:background="#fd8d8d" 
  25.          android:textColor="#000000" 
  26.          android:layout_margin="4px" 
  27.        > 
  28.        </TextView> 
  29.    </TableLayout> 
  30.     
  31.    <TableLayout  
  32.        android:id="@+id/TableLayout02"  
  33.        android:layout_width="fill_parent"  
  34.        android:layout_height="wrap_content"   
  35.        android:background="#FFFFFF" 
  36.        android:stretchColumns="0" 
  37.        xmlns:android="http://schemas.android.com/apk/res/android"> 
  38.        <!-- android:stretchColumns="0" 设置0号列为可伸展的列,当有多个列可伸展时用逗号隔开 --> 
  39.        <!-- 第二行 --> 
  40.        <TableRow  
  41.          android:id="@+id/TableRow01"  
  42.          android:layout_width="wrap_content"  
  43.          android:layout_height="wrap_content"> 
  44.             <!-- 第一列 --> 
  45.             <TextView  
  46.              android:text="我是被拉上的一列"  
  47.              android:id="@+id/TextView02"  
  48.              android:layout_width="wrap_content"  
  49.              android:layout_height="wrap_content" 
  50.              android:layout_centerInParent="true" 
  51.              android:background="#9cfda3" 
  52.              android:textColor="#000000" 
  53.              android:layout_margin="4px">    
  54.             </TextView>  
  55.             <!-- 第二列 --> 
  56.             <TextView  
  57.              android:text="我的内容少"  
  58.              android:id="@+id/TextView03"  
  59.              android:layout_width="wrap_content"  
  60.              android:layout_height="wrap_content" 
  61.              android:layout_centerInParent="true" 
  62.              android:background="#8d9dfd" 
  63.              android:textColor="#000000" 
  64.              android:layout_margin="4px" 
  65.             >    
  66.             </TextView>          
  67.        </TableRow>  
  68.     </TableLayout> 
  69.      
  70.     <TableLayout  
  71.        android:id="@+id/TableLayout03"  
  72.        android:layout_width="fill_parent"  
  73.        android:layout_height="wrap_content"   
  74.        android:background="#FFFFFF" 
  75.        android:collapseColumns="1" 
  76.        android:shrinkColumns="0" 
  77.        xmlns:android="http://schemas.android.com/apk/res/android"> 
  78.        <!-- android:collapseColumns="1" 隐藏编号为1的列,若有多个列要隐藏,则用逗号隔开,如0,2 --> 
  79.        <!-- android:shrinkColumns="0"  
  80.                     设置0号列为可收缩的列,可收缩的列会纵向扩展 
  81.                     若有多个列要收缩,则用逗号隔开,如0,2 --> 
  82.                      
  83.        <!-- 第三行 --> 
  84.        <TableRow  
  85.          android:id="@+id/TableRow02"  
  86.          android:layout_width="wrap_content"  
  87.          android:layout_height="wrap_content"> 
  88.             <!-- 第一列 --> 
  89.             <TextView  
  90.              android:text="我的被收缩的一列被收缩的一列"  
  91.              android:id="@+id/TextView04"  
  92.              android:layout_width="wrap_content"  
  93.              android:layout_height="wrap_content" 
  94.              android:layout_centerInParent="true" 
  95.              android:background="#9cfda3" 
  96.              android:textColor="#000000" 
  97.              android:layout_margin="4px">    
  98.             </TextView>  
  99.             <!-- 第二列,被设置为隐藏了 --> 
  100.             <TextView  
  101.              android:text="我的内容少"  
  102.              android:id="@+id/TextView05"  
  103.              android:layout_width="wrap_content"  
  104.              android:layout_height="wrap_content" 
  105.              android:layout_centerInParent="true" 
  106.              android:background="#8d9dfd" 
  107.              android:textColor="#000000" 
  108.              android:layout_margin="4px" 
  109.             >    
  110.             </TextView>  
  111.             <!-- 第三列 --> 
  112.             <TextView  
  113.              android:text="我的内容比较长比较长比较长"  
  114.              android:id="@+id/TextView06"  
  115.              android:layout_width="wrap_content"  
  116.              android:layout_height="wrap_content" 
  117.              android:layout_centerInParent="true" 
  118.              android:background="#fd8d8d" 
  119.              android:textColor="#000000" 
  120.              android:layout_margin="4px" 
  121.             >    
  122.             </TextView>          
  123.        </TableRow>    
  124.     </TableLayout>   
  125. </LinearLayout> 

Activity代码为:

 
 
 
 
  1. package com.sunchis; 
  2.  
  3. import android.app.Activity; 
  4. import android.os.Bundle; 
  5.  
  6. public class Android extends Activity {  
  7.     @Override 
  8.     public void onCreate(Bundle savedInstanceState) { 
  9.         super.onCreate(savedInstanceState); 
  10.         setContentView(R.layout.main);          //设置屏幕 
  11.     } 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值