我想动态添加表行...... 以下是我的Java代码:动态行添加到TableLayout
TableLayout tl = (TableLayout) findViewById(R.id.tablerow);
/* Create a new row to be added. */
TableRow tr = new TableRow(this);
tr.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
/* Create a Button to be the row-content. */
Button b = new Button(this);
b.setText("Dynamic Button");
b.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
/* Add Button to row. */
tr.addView(b);
/* Add row to TableLayout. */
tl.addView(tr, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
我的XML代码是
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tablerow"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TableRow>
<TextView
android:id="@+id/content"
android:singleLine="false" />
</TableRow>
</TableLayout>
但我在这条线上得到错误:
final TableLayout tl = (TableLayout)findViewById(R.id.tablerow);
我得到的错误是无法从视图中投射到TableLayout
任何帮助将不胜感激
为什么使用动态充气? setContentView()应该已经做了所有必要的充气。 – 2010-11-25 11:05:57