您的位置:首页 > 移动开发 > Android开发

【Android】界面布局之TableLayout(表格布局)

2012-09-27 22:59 866 查看

  表格布局模型以行列的形式管理子控件,每一行为一个TableRow的对象,当然也可以是一个View的对象。TableRow可以添加子控件,每添加一个为一列。

  android:layout_colum官方解释:The index of the column in which this child should be,也即是设置该控件在TableRow中所处的列。

  android:layout_span官方解释:Defines how many columns this child should span,也即是设置该控件所跨越的列数。

  android:collapseColumns官方解释:The 0 based index of the columns to collapse. The column indices must be separated by a comma: 1, 2, 5.也即是将TableLayout里面指定的列隐藏,若有多列需要隐藏,请用逗号将需要隐藏的列序号隔开。

  android:stretchColumns官方解释:The 0 based index of the columns to stretch. The column indices must be separated by a comma: 1, 2, 5. You can stretch all columns by using the value "*" instead. Note that a column can be marked stretchable and shrinkable at the
same time.也即是设置指定的列为可伸展的列,可伸展的列会尽量伸展以填满所有可用的空间,若有多列需要设置为可伸展,请用逗号将需要伸展的列序号隔开。

  android:shrinkColumns官方解释:The 0 based index of the columns to shrink. The column indices must be separated by a comma: 1, 2, 5. You can shrink all columns by using the value "*" instead. 设置指定的列为可收缩的列。当可收缩的列太宽以至于让其他列显示不全时,会纵向延伸空间。当需要设置多列为可收缩时,将列序号用逗号隔开。

  下面用一个例子简单说明TableLayout的用法:

  

  http://schemas.android.com/apk/res/Android"

  android:layout_width="fill_parent"

  android:layout_height="fill_parent"

  android:stretchColumns="1">

  

  <TEXTVIEW

  android:layout_column="1"

  android:text="打开..."

  android:padding="3dip" />

  <TEXTVIEW

  android:text="Ctrl-O"

  android:gravity="right"

  android:padding="3dip" />

  

  

  <TEXTVIEW

  android:layout_column="1"

  android:text="保存..."

  android:padding="3dip" />

  <TEXTVIEW

  android:text="Ctrl-S"

  android:gravity="right"

  android:padding="3dip" />

  

  

  <TEXTVIEW

  android:layout_column="1"

  android:text="另存为..."

  android:padding="3dip" />

  <TEXTVIEW

  android:text="Ctrl-Shift-S"

  android:gravity="right"

  android:padding="3dip" />

  

  <VIEW

  android:layout_height="2dip"

  android:background="#FF909090" />

  

  <TEXTVIEW

  android:text="*"

  android:padding="3dip" />

  <TEXTVIEW

  android:text="导入..."

  android:padding="3dip" />

  

  

  <TEXTVIEW

  android:text="*"

  android:padding="3dip" />

  <TEXTVIEW

  android:text="导出..."

  android:padding="3dip" />

  <TEXTVIEW

  android:text="Ctrl-E"

  android:gravity="right"

  android:padding="3dip" />

  

  <VIEW

  android:layout_height="2dip"

  android:background="#FF909090" />

  

  <TEXTVIEW

  android:layout_column="1"

  android:text="退出"

  android:padding="3dip" />

  

  

  对应的模拟器效果图如下所示:





  从效果图可以看出,该布局总共有三列,第一个TableRow里面的控件,从第1列开始布局。如果去掉所有控件的属性” android:layout_column="1"”,显示效果如下所示(总共3列,也即第0、1、2列,每一列都用虚线框标示):





  另外一个例子

  布局文件如下所示:

  

  http://schemas.android.com/apk/res/Android"

  android:orientation="vertical"

  android:layout_width="fill_parent"

  android:layout_height="wrap_content"

  >

  <TABLELAYOUT

  android:layout_width="fill_parent"

  android:layout_height="wrap_content"

  android:collapseColumns="1" >

  <TEXTVIEW

  android:text="表一"

  android:gravity="center"

  />

  

  <TEXTVIEW

  android:text="列0"

  android:background="@drawable/dot"/>

  <TEXTVIEW

  android:text="列1"

  android:background="@drawable/dot"/>

  

  

  <TABLELAYOUT

  android:layout_width="fill_parent"

  android:layout_height="wrap_content"

  android:stretchColumns="1">  将列序号用逗号隔开,例如:android:stretchColumns=“0,1”-->

  <TEXTVIEW

  android:text="表二"

  android:gravity="center" />

  

  <TEXTVIEW

  android:text="列0不能伸展"

  android:background="@drawable/dot"/>

  <TEXTVIEW

  android:text="列1可以伸展"

  android:gravity="right"

  android:background="@drawable/dot"/>

  

  

  <TABLELAYOUT

  android:layout_width="fill_parent"

  android:layout_height="wrap_content" >

  <TEXTVIEW

  android:text="表三"

  android:gravity="center" />

  

  <TEXTVIEW

  android:text="这一列很长,将会造成下一列无法显示或显示不全"

  android:background="@drawable/dot" />

  <TEXTVIEW

  android:text="这一列被挤到了屏幕外"

  android:background="@drawable/dot"/>

  

  

  <TABLELAYOUT

  android:layout_width="fill_parent"

  android:layout_height="wrap_content"

  android:shrinkColumns="0">  当需要设置多列为可收缩时,将列序号用逗号隔开,例如:android:shrinkColumns=“0,1”-->

  <TEXTVIEW

  android:text="表四"

  android:gravity="center"

  />

  

  <TEXTVIEW

  android:text="由于设置成了可收缩,所以这一列不管有多长都不会把其他列挤出去"

  android:background="@drawable/dot" />

  <TEXTVIEW

  android:text="这一列会被显示完全"

  android:background="@drawable/dot" />

  

  

  

  对应的模拟器效果图如下所示:





  布局分析:最外层用了一个垂直的linearLayout来放置里面的4个TableLayout,每个TableLayout由两行构成,分别是一个TextView和一个TableRow,每一个TableRow都由两个TextView组成了两列。

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: