您的位置:首页 > 其它

TableLayout表格布局

2016-03-14 21:58 330 查看
                     表格布局模型以行列的形式管理子控件,每一行为一个TableRow的对象,当然也可以是一个View的对象。TableRow可以添加子控件,每添加一个为一列。

TableLayout属性:

  android:collapseColumns:将TableLayout里面指定的列隐藏,若有多列需要隐藏,请用逗号将需要隐藏的列序号隔开。             

  android:stretchColumns:设置指定的列为可伸展的列,以填满剩下的多余空白空间,若有多列需要设置为可伸展,请用逗号将需要伸展的列序号隔开。                

  android:shrinkColumns:设置指定的列为可收缩的列。当可收缩的列太宽(内容过多)不会被挤出屏幕。当需要设置多列为可收缩时,将列序号用逗号隔开。

  android:layout_colum:设置该控件在TableRow中指定的列。

  android:layout_span:设置该控件所跨越的列数.

具体看实现的代码,代码里面写了注释,可以对照运行结果进行分析

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="下面是三个TableLayout" />
<!--定义一个表格,2列可以收缩,3列可以拉伸-->
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:shrinkColumns="1"
android:stretchColumns="2">
<TableRow>
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="正常情况"/>
<Button android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="收缩了的按钮收缩了的按钮收缩了的按钮"/>

<Button android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="可以被拉伸可以被拉伸"/>
</TableRow>
</TableLayout>
<!--android:collapseColumns隐藏某列属性-->
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:collapseColumns="2">
<TableRow>
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="正常情况"/>
<Button android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="我被隐藏了"/>
<!--根据索引值第三个按钮才是隐藏的-->
<Button android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="正常"/>
</TableRow>
</TableLayout>

<!--填充剩下的空间-->
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:stretchColumns="1">
<TableRow>
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="正常情况"/>
<Button android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="我横跨2列"/>
</TableRow>
</TableLayout>

</LinearLayout>

运行截图

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