0

i am doing exactly what this page says: http://en.androidwiki.com/wiki/Dynamically_adding_rows_to_TableLayout

main code:

this.setContentView(R.layout.main);

      /* Find Tablelayout defined in main.xml */
      TableLayout tl = (TableLayout)findViewById(R.id.myTableLayout);
           /* 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)); <code>

xml:

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

but when i duplicate the line tr.addView(b);

it gives me fc. Any idea where is the error?

4

1 回答 1

0

那是因为您要添加相同的按钮。尝试:

            .....
            /* Create a Button to be the row-content. */
            Button b = new Button(this);
            b.setText("Dynamic Button");
            b.setLayoutParams(new LayoutParams(
                      LayoutParams.WRAP_CONTENT,
                      LayoutParams.WRAP_CONTENT));
            /* Add Button to row. */
            tr.addView(b);

            b = new Button(this);
            b.setText("Another Dynamic Button");
            b.setLayoutParams(new LayoutParams(
                      LayoutParams.WRAP_CONTENT,
                      LayoutParams.WRAP_CONTENT));
            /* Add Button to row. */
            tr.addView(b);   

            ......
于 2012-04-09T16:06:40.117 回答