You can build your table rows via XML parts and LayoutInflater. Say you had this as your table_cell.xml:
<TextView android:id="@+id/text"
android:text="woot" />
And this as your table_row.xml (unless you're doing something fancy with your TableRow, you may not need to put it in it's own XML file, and instead just create it programmatically. The result will be the same):
<TableRow />
Assuming your TableLayout reference was called "table", you could do something like this:
TableLayout table = (TableLayout)findViewById(R.id.table);
LayoutInflater inflater = getLayoutInflater();
for(int i = 0; i < 5; i++) {
TableRow row = (TableRow)inflater.inflate(R.layout.table_row, table, false);
View v = (View)inflater.inflate(R.layout.table_cell, row, false);
row.addView(v);
v = (View)inflater.inflate(R.layout.table_cell, row, false);
row.addView(v);
// you can store your reference to `row` here for later use
table.addView(row);
}
With this technique, you can still set up your layout in XML, making it easier to read/organize/edit, and you still have programmatic control over how many columns and rows are in the table. You can also store references to each table row for later use.
synic
2010-04-28 16:29:34