Android五种布局管理器之『TableLayout』
2013-03-26 10:49
585 查看
表格布局(TableLayout)类以行和列的形式管理控件,每一行为一个TableRow对象,也可以作为一个View对象;当为View对象时,该View对象将跨越该行的所有列。在TableRow中可以添加子控件,每添加一个子控件即为一列。
TableLayout布局中并不会为每一行、每一列或每个单元格绘制边框,每一行可以有0个或多个单元格,每个单元格为一个View对象。TableLayout中可以有空的单元格,单元格也可以像HTML中那样跨越多个列。
在TableLayout布局中,一个列的宽度由该列中最宽的那个单元格指定,而表格的宽度是由父容器指定的。在TableLayout中,可以为列设置三种属性:
Shrinkable:如果一个列被标识为Shrinkable,则该列的宽度可以进行收缩,以使表格能够适应其父容器的大小。
Stretchable:如果一个列被标识为Stretchable,则该列的宽度可以进行拉伸,以使填满表格中的空闲空间。
Collapsed:如果一个列被标识为Collapsed,则该列会被隐藏。
注意:一个列可以同时具有Shrinkable属性和Stretchable属性,在这种情况下,该列的宽度将任意拉伸或收缩以适应父容器。
TableLayout继承自LinearLayout类,除了继承来自父类的属性和方法,TableLayout类中还包含表格布局所特有的属性和方法,如下表:
注意:TableLayout中所谓的列序号是从0开始计算的。setShrinkAllColumns和setStretchAllColumns实现的功能是将表格中的所有列设置为Shrinkable或Stretchable。
下面来看一下效果:
其中Main.xml代码如下:
Activity代码为:
TableLayout布局中并不会为每一行、每一列或每个单元格绘制边框,每一行可以有0个或多个单元格,每个单元格为一个View对象。TableLayout中可以有空的单元格,单元格也可以像HTML中那样跨越多个列。
在TableLayout布局中,一个列的宽度由该列中最宽的那个单元格指定,而表格的宽度是由父容器指定的。在TableLayout中,可以为列设置三种属性:
Shrinkable:如果一个列被标识为Shrinkable,则该列的宽度可以进行收缩,以使表格能够适应其父容器的大小。
Stretchable:如果一个列被标识为Stretchable,则该列的宽度可以进行拉伸,以使填满表格中的空闲空间。
Collapsed:如果一个列被标识为Collapsed,则该列会被隐藏。
注意:一个列可以同时具有Shrinkable属性和Stretchable属性,在这种情况下,该列的宽度将任意拉伸或收缩以适应父容器。
TableLayout继承自LinearLayout类,除了继承来自父类的属性和方法,TableLayout类中还包含表格布局所特有的属性和方法,如下表:
属性名称 | 对应方法 | 描述 |
android:collapseColumns | setColumnCollapsed(int,boolean) | 设置指定列号的列属性为Collapsed |
android:shrinkColumns | setShrinkAllColumns(boolean) | 设置指定列号的列属性为Shrinkable |
android:stretchColumns | setStretchAllColumns(boolean) | 设置指定列号的列属性为Stretchable |
下面来看一下效果:
其中Main.xml代码如下:
view plain copy<?xml version="1.0" encoding="utf-8"?> <LinearLayout android:id="@+id/LinearLayout01" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:background="@drawable/android" android:gravity="bottom"> <!-- 第一行 --> <TableLayout android:id="@+id/TableLayout01" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="#FFFFFF" xmlns:android="http://schemas.android.com/apk/res/android"> <TextView android:text="我是单独的一行" android:id="@+id/TextView01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:background="#fd8d8d" android:textColor="#000000" android:layout_margin="4px" > </TextView> </TableLayout> <TableLayout android:id="@+id/TableLayout02" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="#FFFFFF" android:stretchColumns="0" xmlns:android="http://schemas.android.com/apk/res/android"> <!-- android:stretchColumns="0" 设置0号列为可伸展的列,当有多个列可伸展时用逗号隔开 --> <!-- 第二行 --> <TableRow android:id="@+id/TableRow01" android:layout_width="wrap_content" android:layout_height="wrap_content"> <!-- 第一列 --> <TextView android:text="我是被拉上的一列" android:id="@+id/TextView02" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:background="#9cfda3" android:textColor="#000000" android:layout_margin="4px"> </TextView> <!-- 第二列 --> <TextView android:text="我的内容少" android:id="@+id/TextView03" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:background="#8d9dfd" android:textColor="#000000" android:layout_margin="4px" > </TextView> </TableRow> </TableLayout> <TableLayout android:id="@+id/TableLayout03" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="#FFFFFF" android:collapseColumns="1" android:shrinkColumns="0" xmlns:android="http://schemas.android.com/apk/res/android"> <!-- android:collapseColumns="1" 隐藏编号为1的列,若有多个列要隐藏,则用逗号隔开,如0,2 --> <!-- android:shrinkColumns="0" 设置0号列为可收缩的列,可收缩的列会纵向扩展 若有多个列要收缩,则用逗号隔开,如0,2 --> <!-- 第三行 --> <TableRow android:id="@+id/TableRow02" android:layout_width="wrap_content" android:layout_height="wrap_content"> <!-- 第一列 --> <TextView android:text="我的被收缩的一列被收缩的一列" android:id="@+id/TextView04" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:background="#9cfda3" android:textColor="#000000" android:layout_margin="4px"> </TextView> <!-- 第二列,被设置为隐藏了 --> <TextView android:text="我的内容少" android:id="@+id/TextView05" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:background="#8d9dfd" android:textColor="#000000" android:layout_margin="4px" > </TextView> <!-- 第三列 --> <TextView android:text="我的内容比较长比较长比较长" android:id="@+id/TextView06" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:background="#fd8d8d" android:textColor="#000000" android:layout_margin="4px" > </TextView> </TableRow> </TableLayout> </LinearLayout>
Activity代码为:
view plain copypackage com.sunchis; import android.app.Activity; import android.os.Bundle; public class Android extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //设置屏幕 } }
相关文章推荐
- Android五种布局管理器之『TableLayout』
- Android开发5:布局管理器2(表格布局TableLayout)
- Android开发5:布局管理器2(表格布局TableLayout)
- Android五种布局方式——LinearLayout、RelativeLayout、TableLayout....(四)
- Android五种布局管理器之『AbsoluteLayout』
- Android布局方式之表格布局管理器(TableLayout)
- Android五种布局管理器之『AbsoluteLayout』
- android布局管理器之TableLayout
- 【Android】7.4TableLayout(表格布局)
- android:TableLayout表格布局
- (转)浅谈Android五大布局——LinearLayout、FrameLayout、AbsoulteLayout、RelativeLayout和TableLayout
- Android基础教程(二)之五大布局对象---FrameLayout,LinearLayout ,AbsoluteLayout,RelativeLayout,TableLayout.
- android:TableLayout表格布局详解
- Android学习(十五)五布局之表格布局TableLayout
- Android开发7:布局管理器4(网格布局GridLayout,绝对布局AbsoluteLayout)
- Android开发之TableLayout布局的使…
- 浅谈Android五大布局(一)——LinearLayout、FrameLayout和AbsoulteLayout,RelativeLayout和TableLayout
- android:TableLayout表格布局详解
- [置顶] Android--(8)--详解表格布局(TableLayout)
- [Android步步为营]第4营 布局管理器Layout