类结构图:

java.lang.Object

android.view.View

android.view.ViewGroup

android.widget.LinearLayout

android.widget.TableLayout

TableLayout没有边框的,它是由多个TableRow对象组成,每个TableRow可以 有0个或多个单元格,每个单元格就是一个View。这些TableRow,单元格不能设 置layout_width,宽度默认是fill_parent的,只有高度layout_height可以自定 义,默认是wrap_content。

android:shrinkColumns设置可以收缩的列号,android:stretchColumns设置 可以伸展的列号。需要注意的是列号从0开始,也可以用”*”表示指定所有的列 。

TableLayout中的最大列数是所有的位于TableLayout中的TableRow的列数中 的最大值,即列数最多的一行有多少列,TableLayout就有多少列。

XML属性

相应方法

描述

android:collapseColumns

setColumnCollapsed (int columnIndex, boolean isCollapsed)

设置表格的列是否隐藏

android:shrinkColumns

setShrinkAllColumns (boolean shrinkAllColumns)

作用:设置表格的列是否收缩(列编号从0开始,下同),多列用逗号隔开(下同),如android:shrinkColumns="0,1,2",即表格的第1、2、3列的内容是收缩的以适合屏幕,不会挤出屏幕。

android:stretchColumns

setStretchAllColumns (boolean stretchAllColumns)

设置表格的列是否拉伸

实践:

布局文件:

<?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">    <TableLayout android:stretchColumns="1"      android:layout_width="fill_parent" android:layout_height="wrap_content">      <TableRow>        <TextView android:layout_width="wrap_content"           android:layout_height="wrap_content" android:text="姓名" />        <EditText android:text="" android:layout_width="fill_parent"           android:layout_height="wrap_content" />      </TableRow>      <TableRow>        <TextView android:layout_width="wrap_content"           android:layout_height="wrap_content" android:text="密码 " />        <EditText android:text="" android:layout_width="fill_parent"           android:layout_height="wrap_content" />      </TableRow>      <TableRow>        <Button android:text="取消" android:layout_width="wrap_content"           android:layout_height="wrap_content" />        <Button android:text="取消" android:layout_width="fill_parent"           android:layout_height="wrap_content" />      </TableRow>   </TableLayout></LinearLayout>


这个关于用到了android:stretchColumns它是指定哪一列被拉伸[从0开始],在这里我们指定了为了1,所以出现如下的效果图:

关于动态添加设置TableLayout官方并不推荐所以也不在此阐述,所以在实际开发中,我们一般只用XML来设置布局

更多相关文章

  1. android GLSurfaceView匹配屏幕宽度及视频宽高比设置view大小
  2. Android可拖拽布局ConstraintLayout
  3. Android(安卓)TabLayout+ViewPager+Fragment简单实现
  4. Android:横屏时禁止输入法全屏
  5. Binary XML file line #11: You must supply a layout_width att
  6. android状态栏显示进度
  7. 改变ListView拖动时有黑色的阴影
  8. Android基于ViewPager+Fragment实现左右滑屏效果的方法
  9. android之Layout(二)

随机推荐

  1. Android调试之TraceView
  2. 2011android面试题目及其答案大全
  3. 【Android每日一讲】2012.11.01 程序加载
  4. 为包含工作线程Android程序编写稳定的ins
  5. Android用itext生成含中文的PDF文档
  6. Android短信息验证码自动填写详细介绍
  7. Android的Ruby解释器
  8. Android发送短信、打电话、发送邮件的程
  9. Delphi XE5 for Android(安卓)(一)
  10. android开发笔记(一)Android(安卓)studio