Android中文API(116)——TableLayout

前言html

  本章内容是android.widget.TableLayout,版本为Android 3.0 r1,翻译来自"cnmahj",欢迎你们访问他的博客:http://android.toolib.net/blog/,再次感谢"cnmahj"!欢迎你一块儿参与Android的中文翻译,联系我over140@gmail.com。
 java

 

声明android

  欢迎转载,但请保留文章原始出处:)
 安全

 

    农民伯伯:http://over140.blog.51cto.com/ide

    Android中文翻译组: http://goo.gl/6vJQl

 

 

 

正文函数

  1、结构布局

 

public class TableLayout extends LinerLayout学习

        

java.lang.Object编码

         android.view.Viewspa

               android.view.ViewGroup

                      android.widget.LinearLayout

                             android.widget.TableLayout

 

 

  2、概述
 

 

  按照行列来组织子视图的布局。表格布局包含一系列的 TableRow对象,用于定义行(实际上你也能够使用其它子对象,将在后面进行解释)。表格布局不为它的行、列和单元格显示表格线。每一个行能够包含0个以上(包括0)的单元格; 每一个单元格能够设置一个View对象.与行包含不少单元格同样, 表格包含不少列。表格的单元格能够为空.单元格能够象 HTML 那样跨列。

  列的宽度由该列全部行中最宽的一个单元格决定.不过表格布局能够经过 setColumnShrinkable() 方法或者 setColumnStretchable() 方法来标记某些列能够收缩或能够拉伸. 若是标记为能够收缩,列宽能够收缩以使表格适合容器的大小。若是标记为能够拉伸, 列宽能够拉伸以占用多余的空间。表格的总宽度由其父容器决定. 记住列能够同时具备可拉伸和可收缩标记是很重要的。在列能够调整其宽度以占用可用空间, 但不能超过限度时是颇有用的.最后,你能够经过调用setColumnCollapsed() 方法来隐藏列。

  表格布局的子对象不能指定 layout_width 属性.宽度永远是 MATCH_PARENT。不过子对象能够定义 layout_height 属性;其默认值是 WRAP_CONTENT. 若是子对象是 TableRow,其高度永远是 WRAP_CONTENT

  不管是在代码仍是在 XML 布局文件中,单元格必须安装索引顺序加入表格行. 列号是从 0 开始的.若是你不为子单元格指定列号,其将自动增值,使用下一个可用列号. 若是你跳过某个列号,他在表格行中做为空能够改对待。参见 ApiDemos 中经过 XML 建立表格的布局示例。

虽然表格布局典型的子对象是表格行,实际上你能够使用任何视图类的子类, 做为表格视图的直接子对象.视图会做为只有一行并结合了全部列的单元格显示。

  参见

  Table Layout tutorial

 

 

  3、内部类

 

  class        TableLayout.LayoutParams

  该类强制将子视图的宽度设为 MATCH_PARENT 将没有设置高度的子视图的高度设为 WRAP_CONTENT

 

 

  4、XML属性

 

属性名称

描述

android:collapseColumns

隐藏从0开始的索引列。列直接必须用逗号隔开:1, 2, 5。非法或重复的设置将被忽略。

相关方法

setColumnCollapsed(int,boolean)

android:shrinkColumns

收缩从0开始的索引列。列直接必须用逗号隔开:1, 2, 5。非法或重复的设置将被忽略。你能够经过"*"代替收缩全部列。注意一列能同时表示收缩和拉伸。

相关方法

setColumnCollapsed(int,boolean)

android:stretchColumns

拉伸从0开始的索引列。列直接必须用逗号隔开:1, 2, 5。非法或重复的设置将被忽略。你能够经过"*"代替收缩全部列。注意一列能同时表示收缩和拉伸。

相关方法

setColumnCollapsed(int,boolean)

 

 

  5、构造函数

 

  public TableLayout (Context context)

  为给定的上下文建立表格布局。

             参数

  context  应用程序上下文

 

  public TableLayout (Context context, AttributeSet attrs)

  使用指定的属性集合为给定的上下文建立表格布局。

             参数

  context  应用程序上下文

  attrs        属性集合

 

 

  6、公共方法

 

  public void addView (View child)

  添加子视图。若是子视图没有设置布局参数,则使用视图组(ViewGroup)的布局参数为该视图布局。

             参数

  child         添加的子视图

 

  public void addView (View child, int index)

  添加子视图。若是子视图没有设置布局参数,则使用视图组(ViewGroup)的布局参数为该视图布局。

             参数

  child         添加的子视图

  index       子视图加入的位置索引

 

  public void addView (View child, int index, ViewGroup.LayoutParams params)

  用指定的布局参数添加一个子视图。

             参数

  child         添加的子视图

  index       子视图加入的位置索引

  params  为子视图指定得布局参数

 

  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

 

 

  7、受保护方法

 

  protected boolean checkLayoutParams (ViewGroup.LayoutParams p)

    (译者注:检测是否是AbsoluteLayout.LayoutParams的实例)

 

  protected LinearLayout.LayoutParams generateDefaultLayoutParams ()

  返回宽度为MATCH_PARENT 高度为WRAP_CONTENT的布局参数集合。

             返回值

  默认布局参数集合或空

 

  protected LinearLayout.LayoutParams generateLayoutParams (ViewGroup.LayoutParams p)

  基于提供的布局参数返回一组安全的布局参数集合.当传入 ViewGroup 的视图的参数没有经过checkLayoutParams(android.view.ViewGroup.LayoutParams) 的检测时,调用该方法. 该方法会返回适合 ViewGroup 的新的布局参数,可能从指定的布局参数中复制适当的属性。

             参数

  p      要转换为适合于 ViewGroup 的布局参数的集合

                返回值

                       ViewGroup.LayoutParams或其子类的实例

 

  protected void onLayout (boolean changed, int l, int t, int r, int b)

  该视图设置其子视图的大小及位置时调用.派生类能够重写此方法,并为其子类布局。

             参数

  changed           是否为视图设置了新的大小和位置

  l       相对于父视图的左侧的位置

  t      相对于父视图的顶部的位置

  r      相对于父视图的右侧的位置

  b      相对于父视图的底部的位置

 

  protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec)

  评估视图及其内容,以决定其宽度和高度.此方法由measure(int, int) 调用,子类能够重载以提供更精确、更有效率的衡量其内容尺寸的方法。

  约定: 覆盖该方法时,必须调用 setMeasuredDimension(int, int) 方法来保存评估结果的视图的宽度和高度.若是忘记将致使 measure(int, int) 方法抛出IllegalStateException异常.要有效的利用父类的onMeasure(int, int)方法。

  基类测量的是背景的大小,除非 MeasureSpec 容许超过背景.子类应该重写 onMeasure(int, int) 方法,觉得其内容提供更适合的大小。

  若是重写了该方法,子类要确保其高度和宽度大于等于视图的最小高度和宽度. getSuggestedMinimumHeight() getSuggestedMinimumWidth()

             参数

  widthMeasureSpec         父视图要求的横向空间大小.该要求由 View.MeasureSpec 进行了编码处理。

         heightMeasureSpec        父视图要求的纵向空间大小 . 该要求由 View.MeasureSpec 进行了编码处理。

 

 

  8、补充
 

 

    文章精选

                   Android UI学习 - TableLayout

 

 

 

TableLayout.LayoutParams

译者署名: madgoat

译者连接: http://madgoat.cn

版本:Android 3.0 r1

 

结构

继承关系

public static class TableLayout.LayoutParams extends LinerLayout.LayoutParams

        

java.lang.Object

         android.view.ViewGroup.LayoutParams

               android.view.ViewGroup.MarginLayoutParams

                      android.widget.LinearLayout.LayoutParams

                             android.widget.TableLayout.LayoutParams

 

概述

         这种布局参数强制每一个子元素的宽度为MATCH_PARENT每一个子元素的高度为WRAP_CONTENT,但只有在未指定高度的状况下。

 

构造函数

  public TableLayout.LayoutParams (Context c, AttributeSet attrs)

 

public TableLayout.LayoutParams (int w, int h)

 

public TableLayout.LayoutParams (int w, int h, float initWeight)

 

public TableLayout.LayoutParams ()

设置子元素的宽为ViewGroup.LayoutParams高为WRAP_CONTENT

 

public TableLayout.LayoutParams (ViewGroup.LayoutParams p)

 

public TableLayout.LayoutParams (ViewGroup.MarginLayoutParams source)

 

公共方法

protected void setBaseAttributes (TypedArray a, int widthAttr, int heightAttr)

修改行宽度的参数为MATCH_PARENT行高度的布局参数若是没有指定则修改成WRAP_CONTENT

参数

a      一组样式属性

widthAttr        获取的宽度属性

heightAttr       获取的高度属性

相关文章
相关标签/搜索