我的代码:
public class Test extends Activity {
private TableLayout tableLayout = null;
private Button add = null;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
tableLayout = (TableLayout)findViewById(R.id.tableLayout);
add = (Button)findViewById(R.id.agregar);
add.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
TableRow fila = new TableRow(Test.this);
Button eliminar = new Button(Test.this);
eliminar.setText("delete");
eliminar.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
fila.addView(eliminar,new TableLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
tableLayout.addView(fila,new TableLayout.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
}
});
}
我希望它在每次单击 add 时添加一个新行 Button
。我的xml如下:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
android:id="@+id/relativelayout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TableLayout
android:id="@+id/tableLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TableRow
android:id="@+id/tableRow1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textView1"
android:text="Column 1"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="@+id/button1"
android:text="Delete" />
</TableRow>
</TableLayout>
<Button
android:id="@+id/agregar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/tableLayout"
android:text="Add" />
</RelativeLayout>
每次我点击Button
,它什么都不做。有人可以帮我吗?