Table Layout

A TableLayout is just what you'd expect: it lays out the included Views in the form of a table (similar to an HTML table). We can create a table of TextViews to show how you would create that kind of screen for an application. Here's an example TableLayout XML file:

<?xml version="l.0" encoding="utf-8"?>

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/tblJobs" android:layout_width="fill_parent"

android:layout_height="wrap_content" >

<TableRow android:layout_width="fill_parent" android:layout_height="wrap_content"> <Button android:text="Cell 11" android:id="@+id/btnCelll" android:layout_width="20dip" android:layout_height="wrap_content" />

<TextView android:id="@+id/txtCelll2"

android:layout_width="20dip"

android:layout_height="wrap_content"

android:text="Cell 12" />

<TextView android:id="@+id/txtCelll3"

android:layout_width="20dip"

android:layout_height="wrap_content"

android:text="Cell 13" />

<TextView android:id="@+id/txtCell14"

android:layout_width="20dip"

android:layout_height="wrap_content"

android:text="Cell 14" />

</TableRow> <TableRow android:layout_width="fill_parent" android:layout_height="wrap_content"> <Button android:text="Cell 21" android:id="@+id/btnCo21" android:layout_width="80dip" android:layout_height="wrap_content" />

<TextView android:id="@+id/txtCell22"

android:layout_width="80dip"

android:layout_height="wrap_content"

android:text="Cell 22" />

<TextView android:id="@+id/txtCell23"

android:layout_width="80dip"

android:layout_height="wrap_content"

android:text="Cell 23"

<TextView android:id="@+id/txtCell24"

android:layout_width="80dip"

android:layout_height="wrap_content"

android:text="Cell 24"

</TableRow> </TableLayout>

Figure 11-14 shows the resulting layout on the emulator screen.

Figure 11-14. TableLayout

The structure of the XML file is pretty evident: the TableLayout tags contain a list of TableRows that in turn contain the Views you want to appear on each line of the table. Notice that the layout_width values are different in the two rows—all the widths in the first row are specified as 20dip, whereas the widths in the second row are specified as

28dip—yet the columns line up on the screen. To preserve the look of a table, Android makes each column as wide as the widest cell in that column.

Of course, the cells are addressable from your Java code, and you can add rows pro-grammatically to the table, if that's what your application needs to do.

0 0

Post a comment