下面这段代码动态生成表格1*3表格,每个表格里添加一个按钮和一个文体,运行效果如下图

java代码如下:
TableLayout tableLayout = (TableLayout)findViewById(R.id.TableLayout01);
tableLayout.setStretchAllColumns(true);
for (int row=0;row<1;row++){
TableRow tablerow = new TableRow(MyLocationUser.this);
tablerow.setBackgroundColor(Color.rgb(222, 220, 210));
tablerow.setOrientation(TableRow.VERTICAL);
for (int col=0;col<3;col++){
Button bt = new Button(MyLocationUser.this);
bt.setText("按钮");
tablerow.addView(bt);
TextView tv= new TextView(MyLocationUser.this);
tv.setText("文本");
tablerow.addView(tv);
}
tableLayout.addView(tablerow,new TableLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
}
xml代码如下:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/bt_dttable"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="动态生成表格"
android:onClick="dttable"
/>
<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:stretchColumns="*"
android:background="#dedcd2">
<TableRow
android:background="#dedcd2"
android:layout_margin="0.5dip">
<TextView