1page.title=Hello, TableLayout 2parent.title=Hello, Views 3parent.link=index.html 4@jd:body 5 6<p>A {@link android.widget.TableLayout} is a ViewGroup that 7will lay child View elements into rows and columns.</p> 8 9 10<ol> 11 <li>Start a new project/Activity called HelloTableLayout.</li> 12 <li>Open the layout file. 13 Make it like so: 14<pre> 15<?xml version="1.0" encoding="utf-8"?> 16<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 17 android:layout_width="fill_parent" 18 android:layout_height="fill_parent" 19 android:stretchColumns="1"> 20 21 <TableRow> 22 <TextView 23 android:layout_column="1" 24 android:text="Open..." 25 android:padding="3dip" /> 26 <TextView 27 android:text="Ctrl-O" 28 android:gravity="right" 29 android:padding="3dip" /> 30 </TableRow> 31 32 <TableRow> 33 <TextView 34 android:layout_column="1" 35 android:text="Save..." 36 android:padding="3dip" /> 37 <TextView 38 android:text="Ctrl-S" 39 android:gravity="right" 40 android:padding="3dip" /> 41 </TableRow> 42 43 <TableRow> 44 <TextView 45 android:layout_column="1" 46 android:text="Save As..." 47 android:padding="3dip" /> 48 <TextView 49 android:text="Ctrl-Shift-S" 50 android:gravity="right" 51 android:padding="3dip" /> 52 </TableRow> 53 54 <View 55 android:layout_height="2dip" 56 android:background="#FF909090" /> 57 58 <TableRow> 59 <TextView 60 android:text="X" 61 android:padding="3dip" /> 62 <TextView 63 android:text="Import..." 64 android:padding="3dip" /> 65 </TableRow> 66 67 <TableRow> 68 <TextView 69 android:text="X" 70 android:padding="3dip" /> 71 <TextView 72 android:text="Export..." 73 android:padding="3dip" /> 74 <TextView 75 android:text="Ctrl-E" 76 android:gravity="right" 77 android:padding="3dip" /> 78 </TableRow> 79 80 <View 81 android:layout_height="2dip" 82 android:background="#FF909090" /> 83 84 <TableRow> 85 <TextView 86 android:layout_column="1" 87 android:text="Quit" 88 android:padding="3dip" /> 89 </TableRow> 90</TableLayout> 91</pre> 92<p>Notice how this resembles the structure of an HTML table. <code>TableLayout</code> is like the 93<code>table</code> element; <code>TableRow</code> is like a <code>tr</code> element; but for our cells like 94the html <code>td</code> element, we can use any kind of View. Here, we use <code>TextView</code> for the cells.</p> 95 96</li> 97<li>Make sure your Activity loads this layout in the <code>onCreate()</code> method: 98<pre> 99public void onCreate(Bundle savedInstanceState) { 100 super.onCreate(savedInstanceState); 101 setContentView(R.layout.main); 102} 103</pre> 104<p><code>R.layout.main</code> refers to the <code>main.xml</code> layout file.</p> 105</li> 106<li>Run it.</li> 107</ol> 108<p>You should see the following:</p> 109<img src="images/hello-tablelayout.png" width="150px" /> 110 111<h3>References</h3> 112<ul> 113 <li>{@link android.widget.TableLayout}</li> 114 <li>{@link android.widget.TableRow}</li> 115 <li>{@link android.widget.TextView}</li> 116</ul> 117 118 119