Android dynamically add row to tablelayout

The way to add row to tablelayout in android.

  1. Find the table layout id. 
  2. New a row for table.
  3. Add the view you want.( EditText for this example) 
  4. Add the new EditText to row.
  5. Add the row to tablelayout.
  6. Done
public void touchToAddRow()
{
	TableLayout tl = (TableLayout)findViewById(R.id.tableLayout);

	TableRow tr = new TableRow(this);
	tr.setLayoutParams(new LayoutParams( LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));

	EditText et1 = new EditText(this);
	et1.setId(100+rows*6+0);

	rows++;

	tr.addView(et1);
tl.addView(tr, new LayoutParams( LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
}

0 意見: