Android-tablelayout table layout

Source: Internet
Author: User
<TableLayout xmlns:android=""    android:layout_width="fill_parent" android:layout_height="fill_parent"    android:stretchColumns="1"    android:shrinkColumns="1"    >      <TableRow>        <TextView android:text="Open..."             android:layout_column="1" android:padding="3dip" />        <TextView android:text="Ctrl-O"             android:gravity="right" android:padding="3dip" />    </TableRow>     <TableRow>        <TextView android:text="Save..."            android:layout_column="1" android:padding="3dip" />        <TextView android:text="Ctrl-S"            android:gravity="right" android:padding="3dip" />    </TableRow>     <View        android:layout_height="2dip" android:background="#FF909090" />     <TableRow>        <TextView android:text="X"            android:padding="3dip" />        <TextView android:text="Import..."            android:padding="3dip" />    </TableRow>     <TableRow>        <TextView android:text="X"            android:padding="3dip" />        <TextView android:text="Export..."            android:padding="3dip" />        <TextView android:text="Ctrl-E"            android:gravity="right" android:padding="3dip" />    </TableRow>     <View        android:layout_height="2dip" android:background="#FF909090" />     <TableRow>        <TextView android:text="Quit... And Do A Lot Of Stuff Just To Be Too Long For This Screen Because It Is A Test After All"            android:layout_column="1" android:padding="3dip" />    </TableRow>    <TableRow>        <EditText android:hint="Test Edit" android:layout_span="3"            android:padding="3dip" />    </TableRow></TableLayout>

Call it in an Activity and run it. The effect is as follows:

The effect is good (nonsense, this is an example in the Android API Demo !). Okay, Let's explain it one by one.

How do I place blank cells?
Android: layout_column is used to specify the start position of the cell. The previous cell is automatically left empty. If you want to leave the subsequent cells empty, you can leave them empty. For example:

        <TextView android:text="Open..."             android:layout_column="1" android:padding="3dip" />

How to align right?
Use android: gravity to specify the right alignment of the content in TextView. Note that it is not layout_gravity.
For example:

        <TextView android:text="Ctrl-O"             android:gravity="right" android:padding="3dip" />

Split line? Split line!
Directly add a View in TableLayout. The width of the View is automatically fill_parent, and the height and background color are specified. For example:

    <View        android:layout_height="2dip" android:background="#FF909090" />

How to make one cell cross-column (column span )?
Use android: layout_span to specify the number of columns in Column Span. For example:

        <EditText android:hint="Test Edit" android:layout_span="2"            android:padding="3dip" />

Note: Only controls in TableRow need to use layout_span. The controls directly added to TableLayout automatically span all columns.

How can I enable automatic expansion of certain columns? Like layout_weight in linearlayout?
People who use TableLayout for the first time may habitually use layout_weight, hoping that the View can be automatically extended in TableRow, but the correct method should be to add the attribute android: stretchColumns in the TableLayout definition, the value is the serial number of the column to be automatically expanded. The following is an example.

<TableLayout xmlns:android=""    android:layout_width="fill_parent" android:layout_height="fill_parent"    android:stretchColumns="1"    android:shrinkColumns="1"    >

Are you worried that the content of a column is too large to squeeze other columns or even exceed the range?
Similarly, you can add the android: shrinkColumns attribute to TAbleLayout. The value is the sequence number of the column to be automatically folded.
I tried to remove Android: stretchcolumns and Android: shrinkcolumns in the layout definition and check the effect?


The column number starts from 0.
The elements in TableLayout usually do not need to specify the width and height, especially the width. The width of TableRow or other TableLayout sub-controls is fill_parent, and the height is wrap_content. The width and height of the Child control added to TableRow are wrap_content.
Controls in TableRow are automatically aligned unless you use layout_span.
Data in tablelayout is usually prepared in advance. If you need dynamic data, you 'd better use listview.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.