我想動態添加錶行...... 以下是我的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