在Android中动态地向TableLayout添加行

时间:2011-07-29 11:57:08

标签: java android-layout tablelayout

我正在开发一个应用程序,它需要动态地向具有2列的表添加行。下面是我正在使用的代码....我得到一个“ForceClose”错误... plss help ....

<?xml version="1.0" encoding="utf-8"?>
    <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/table"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TableRow 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
>
<Button android:id="@+id/b1" />
</TableRow>
    </TableLayout>

这是我的java代码

            TableLayout tb=(TableLayout)findViewById(R.id.table);
    TableRow tr=new TableRow(getBaseContext());
    Button b1=(Button)findViewById(R.id.b1);
    LayoutParams lp=new         LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);

    tr.setLayoutParams(lp);
    b1.setLayoutParams(lp);
    b1.setText("I NEED HELP ! SOS ");

    tr.addView(b1);
    tb.addView(tr,lp);

1 个答案:

答案 0 :(得分:0)

tb.addView(tr,lp);有一个错误.. Lp是已经设置为TableRow的布局参数,因此只需使用tb.addView(tr);代替tb.addView(tr,lp);