Android TableLayout表格布局

一:TableLayout是表格布局先上例子:html

定义一个xml布局文件tablelayout.xml:java

内容以下:android

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:gravity="center_horizontal">
  <TableLayout 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:stretchColumns="0,1,2"    
    android:shrinkColumns="1,2"
   >
   <TableRow>        
       <TextView            
           android:layout_column="1"            
           android:text="姓名"            
           android:gravity="center"
           android:background="#BBFFFF"/>        
       <TextView            
           android:text="基本信息"            
           android:gravity="center"
           android:background="#CD00CD"/>    
       <TextView            
           android:text="大大的人人儿呢"            
           android:gravity="center"
           android:background="#A020F0"/>    
    </TableRow>
  </TableLayout>
</LinearLayout>

 

package com.test;

import android.app.Activity;
import android.os.Bundle;

public class TableLayoutAndRowDemo extends Activity {
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tablelayout);
	}

}

效果:app

内容讲解:ide

TableLayout,表格布局采用行列形式管理UI组件,TableLayout不须要明确地声明有多少行和列,而是经过添加TableRow、其它组件来控制表格的行数、列数。
v每次向TableLayout添加一个TableRow,就是在向表格添加一行,TableRow也是容器,能够向TableRow中添加组件,每添加一个组件,便是添加一列。
v若是直接向TableLayout添加组件,则认为这个组件占用一行。
v表格布局中列的宽度便是每一列中最宽的组件的宽度。 函数

 android:collapseColumns="1---隐藏该TableLayout里的TableRow的列1,即第2列(从0开始计算),如有多列要隐藏,用“,”隔开。setColumnCollapsed(int,boolean)布局

android:stretchColumns="0,1,2"----设置列0、一、2为可伸展列。setColumnCollapsed(int,boolean)spa

android:shrinkColumns="1,2"-----设置列一、2为可收缩列。当LayoutRow里面的控件尚未布满布局时,shrinkColumns不起做用 setColumnCollapsed(int,boolean)code

android:background="@drawable/picture_name"----本例中没有涉及此属性,它是要设置当前view 的背景图片,图片文件应该放在res文件夹下。xml

android:layout_column="1":表示控件放在标号为1的列上,标号是从0开始的
android:layout_span表示一个控件占几列空间 
能够用”*”来表示全部列,同一列能够同时设置为shrinkable和stretchable

 函数:

public TableLayout (Context context);
//为给定的上下文建立表格布局。
//参数   context  应用程序上下文

 

public TableLayout (Context context, AttributeSet attrs)
//使用指定的属性集合为给定的上下文建立表格布局。
//参数    context  应用程序上下文     attrs        属性集合

 

public void addView (View child)
public void addView (View child, int index)
public void addView (View child, int index, ViewGroup.LayoutParams params)
//添加子视图。若是子视图没有设置布局参数,则使用视图组(ViewGroup)的布局参数为//该视图布局。index  子视图加入的位置索引
public void addView (View child, ViewGroup.LayoutParams params)
//使用指定的布局参数添加子视图。
//参数    child         添加的子视图   params  设置到子视图上的布局参数
public TableLayout.LayoutParams generateLayoutParams (AttributeSet attrs)
//返回一组基于提供的属性集合的布局参数集合。
//参数
//attrs        用于生成布局参数的属性集
//返回值        ViewGroup.LayoutParams或其子类的实例

 

public boolean isColumnCollapsed (int columnIndex)
//返回指定列的折叠状态。
//参数   columnIndex  列索引
//返回值     折叠时为true;不然为false 

public boolean isColumnShrinkable (int columnIndex)
//返回指定的列是否可收缩。
//参数      columnIndex  列索引
//返回值    若是列能够收缩,返回true;不然返回false

 

public boolean isColumnStretchable (int columnIndex)
//返回指定的列是否可拉伸。
//参数       columnIndex  列索引
//返回值     若是列能够拉伸,返回true;不然返回false

 

public boolean isShrinkAllColumns ()
//指示是否全部的列都是可收缩的。
//返回值
// 若是全部列均可收缩,返回true;不然返回false

 

public boolean isStretchAllColumns ()
//指示是否全部的列都是可拉伸的。
//返回值    若是全部列均可拉伸,返回true;不然返回false

 

public void requestLayout ()
//当某些变动致使视图的布局失效时调用该方法。该方法按照视图树的顺序调用。

 
public void setColumnCollapsed (int columnIndex, boolean isCollapsed)

  //折叠或恢复给定列。折叠时,列从屏幕上消失,其空间由其它列占用。 当列属于 TableRow 时才能够进行折叠/恢复操做。

  //调用该方法会请求布局操做。
相关XML属性    android:collapseColumns
参数    columnIndex  列索引
isCollapsed     折叠时为true;不然为false

 

public void setColumnShrinkable (int columnIndex, boolean isShrinkable)
//设置指定列是否可收缩。当行太宽时,表格能够收缩该列以提供更多空间。
//调用该方法会请求布局操做。
//相关XML属性    android:shrinkColumns
//参数    columnIndex  列索引
//         isShrinkable    若是列能够收缩,设为真;不然设为假。默认是假。

 

public void setColumnStretchable (int columnIndex, boolean isStretchable)
//设置指定列是否可拉伸。可拉伸时,列会尽量多的占用行中的可用空间。
//调用该方法会请求布局操做。
//相关XML属性    android:stretchColumns
//参数    columnIndex  列索引
//         isStretchable 若是列能够拉伸,设为真;不然设为假.默认是假

 

public void setOnHierarchyChangeListener (ViewGroup.OnHierarchyChangeListener listener)
注册当从视图中添加或移除子视图时发生的回调函数。
参数

  listener  层次结构变动时执行的回调函数
public void setShrinkAllColumns (boolean shrinkAllColumns)

  标记全部列为可收缩的便利的方法。

  相关XML属性   android:shrinkColumns
  参数   shrinkAllColumns    若是标记全部列为可收缩时为true

 public void setStretchAllColumns (boolean stretchAllColumns)

  标记全部列为可拉伸的便利的方法。
相关XML属性   android:stretchColumns
参数    stretchAllColumns           若是标记全部列为可拉伸时为true

 三:TableLayout实现边框

  为了醒目,须要给TableLayout设定边框来区分不一样的表格中的信息:

主要是经过设定TableLayout、TableRow 、View颜色反衬出边框的颜色。

例如TableLayout的android:layout_margin="2dip"设置为这个数字 ,在指定一个背景色android:background="#00ff00",它里面的颜色也是这样子的设置,就能够呈现出带边框的效果了。

 (2)关于分割线:
咱们能够经过在两个TableRow之间添加一个<View>,并设置它的layout_height="1dip"和background来解决。但这样就多产生了一个View,有点浪费。
其实还能够设置TableRow的bottomMargin=1来产生一条分割线。 
  3.滚动:

关于TableLayout和HorizontalScrollView一块儿使用时的宽度问题
我有一个TableLayout,它的内容是动态生成的。我遇到了下面的问题:
当动态生成的一行的内容太长时,靠右边的内容会被遮住了。因而我想要这个TableLayout在横向上能够滚动。
解决的办法是,用HorizontalScrollView包装TableLayout,这样,当内容很长时,就会出现横向滚动条。
像这样: 

<?xml version="1.0" encoding="utf-8"?>
<HorizontalScrollView  
 android:layout_width="fill_parent"  
 android:layout_height="fill_parent">  
  <TableLayout  
  android:id="@+id/tl_forcast_result"  
  android:layout_width="fill_parent"  
  android:layout_height="wrap_content"  
  android:padding="10dp">    
  </TableLayout>  
 </HorizontalScrollView>

  但此时又出现了另外一个问题,加上HorizontalScrollView后,虽然我已经设了TableLayout的宽度是fill_parent。但当内容较少时,TableLayout仍是根据内容自适应宽度,不能满屏。 此时,须要设置一个属性就能解决问题了。设置HorizontalScrollView的android:fillViewport="true"。也就是设置是否将HorizontalScrollView的内容宽度拉伸以适应视口(viewport)

相关文章
相关标签/搜索