English | Site Directory

Android - An Open Handset Alliance Project

android.widget
public class

android.widget.TableLayout

java.lang.Object
android.view.View Drawable.Callback KeyEvent.Callback
android.view.ViewGroup ViewManager ViewParent
android.widget.LinearLayout
android.widget.TableLayout

A layout that arranges its children into rows and columns. A TableLayout consists of a number of TableRow objects, each defining a row (actually, you can have other children, which will be explained below). TableLayout containers do not display border lines for their rows, columns, or cells. Each row has zero or more cells; each cell can hold one View object. The table has as many columns as the row with the most cells. A table can leave cells empty. Cells can span columns, as they can in HTML.

The width of a column is defined by the row with the widest cell in that column. However, a TableLayout can specify certain columns as shrinkable or stretchable by calling setColumnShrinkable() or setColumnStretchable(). If marked as shrinkable, the column width can be shrunk to fit the table into its parent object. If marked as stretchable, it can expand in width to fit any extra space. The total width of the table is defined by its parent container. It is important to remember that a column can be both shrinkable and stretchable. In such a situation, the column will change its size to always use up the available space, but never more. Finally, you can hide a column by calling setColumnCollapsed().

The children of a TableLayout cannot specify the layout_width attribute. Width is always FILL_PARENT. However, the layout_height attribute can be defined by a child; default value is WRAP_CONTENT. If the child is a TableRow, then the height is always WRAP_CONTENT.

Cells must be added to a row in increasing column order, both in Java and XML. Column numbers are zero-based. If you don't specify a column number for a child cell, it will autoincrement to the next available column. If you skip a column number, it will be considered an empty cell in that row. See the TableLayout examples in ApiDemos for examples of creating tables in XML.

Although the typical child of a TableLayout is a TableRow, you can actually use any View subclass as a direct child of TableLayout. The View will be displayed as a single row that spans all the table columns.

Nested Classes
TableLayout.LayoutParams

This set of layout parameters enforces the width of each child to be FILL_PARENT and the height of each child to be WRAP_CONTENT, but only if the height is not specified. 

Summary

XML Attributes

Attribute name Related methods  
android:collapseColumns setColumnCollapsed(int,boolean)
 
The 0 based index of the columns to collapse. 
android:shrinkColumns setShrinkAllColumns(boolean)
 
The 0 based index of the columns to shrink. 
android:stretchColumns setStretchAllColumns(boolean)
 
The 0 based index of the columns to stretch. 
XML Attributes inherited from class android.widget.LinearLayout
XML Attributes inherited from class android.view.ViewGroup
XML Attributes inherited from class android.view.View
Constants inherited from class android.widget.LinearLayout
Constants inherited from class android.view.ViewGroup
Constants inherited from class android.view.View
Fields inherited from class android.widget.LinearLayout
Fields inherited from class android.view.ViewGroup
Fields inherited from class android.view.View

Public Constructors

          TableLayout(Context context)

Creates a new TableLayout for the given context.

          TableLayout(Context context, AttributeSet attrs, Map inflateParams)

Creates a new TableLayout for the given context and with the specified set attributes.

Public Methods

        void  addView(View child, LayoutParams params)
Adds a child view with the specified layout parameters.
        void  addView(View child, int index, LayoutParams params)
Adds a child view with the specified layout parameters.
        void  addView(View child)
Adds a child view that already has layout parameters set on it.
        void  addView(View child, int index)
Adds a child view that already has layout parameters set on it.
        LayoutParams  generateLayoutParams(AttributeSet attrs)
        boolean  isColumnCollapsed(int columnIndex)

Returns the collapsed state of the specified column.

        boolean  isColumnShrinkable(int columnIndex)

Returns whether the specified column is shrinkable or not.

        boolean  isColumnStretchable(int columnIndex)

Returns whether the specified column is stretchable or not.

        boolean  isShrinkAllColumns()

Indicates whether all columns are shrinkable or not.

        boolean  isStretchAllColumns()

Indicates whether all columns are stretchable or not.

        void  setColumnCollapsed(int columnIndex, boolean isCollapsed)

Collapses or restores a given column.

        void  setColumnShrinkable(int columnIndex, boolean isShrinkable)

Makes the given column shrinkable or not.

        void  setColumnStretchable(int columnIndex, boolean isStretchable)

Makes the given column stretchable or not.

        void  setOnHierarchyChangeListener(OnHierarchyChangeListener listener)
Register a callback to be invoked when a child is added to or removed from this view.
        void  setShrinkAllColumns(boolean shrinkAllColumns)

Convenience method to mark all columns as shrinkable.

        void  setStretchAllColumns(boolean stretchAllColumns)

Convenience method to mark all columns as stretchable.

Protected Methods

        boolean  checkLayoutParams(LayoutParams p)
        void  measureChildBeforeLayout(View child, int childIndex, int widthMeasureSpec, int totalWidth, int heightMeasureSpec, int totalHeight)

Measure the child according to the parent's measure specs.

        void  measureVertical(int widthMeasureSpec, int heightMeasureSpec)
        void  onLayout(boolean changed, int l, int t, int r, int b)
Called from layout when this view should assign a size and position to each of its children.
        void  onMeasure(int widthMeasureSpec, int heightMeasureSpec)

Measure the view and its content to determine the measured width and the measured height.

Methods inherited from class android.widget.LinearLayout
Methods inherited from class android.view.ViewGroup