0

我试图在单击按钮后将行添加到表中(因此在生成活动之后)。

我的 XML 代码是:

       <TableLayout 
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:stretchColumns="0,1"
            android:id="@+id/result_table">
            <TableRow 
                android:layout_width="fill_parent"
                android:layout_height="wrap_content">
                <TextView
                    style="@style/MyHeader"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Col1" />
                <TextView
                    style="@style/MyHeader"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Col2"  />
            </TableRow>
        </TableLayout>

因此,当我加载活动时,我的桌子上只有我一行。然后当我点击我的按钮时,我会:

    resultTable = (TableLayout)findViewById(R.id.result_table);

    LayoutParams rowLayoutParams = new TableRow.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
    LayoutParams cellLayoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

    TableRow tableRow;
    TextView cellValue;
    for(Asdf a : as) {
        tableRow = new TableRow(this);
        tableRow.setLayoutParams(rowLayoutParams);

        cellValue = new TextView(this);
        cellValue.setText(a.getText());
        cellValue.setLayoutParams(cellLayoutParams);
        cellValue.setTextAppearance(this, R.style.MyText);
        tableRow.addView(cellValue);

        cellValue = new TextView(this);
        cellValue.setText("asdf");
        cellValue.setLayoutParams(cellLayoutParams);
        cellValue.setTextAppearance(this, R.style.MyText);
        tableRow.addView(cellValue);

        resultTable.addView(tableRow);
    }

    resultTable.invalidate();

但它没有添加任何东西,我确信循环会迭代。我在 logcat 中没有收到任何警告/异常。

你能帮我吗 ??

4

1 回答 1

0

尝试像这样更改LayoutParams添加的Views

TableLayout.LayoutParams rowLayoutParams = new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT, TableLayout.LayoutParams.WRAP_CONTENT);
TableRow.LayoutParams cellLayoutParams = TableRow.new LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT);
于 2012-07-01T15:54:23.650 回答