2

从代码中替换表格行时遇到了一个奇怪的问题。

我的 XML 代码如下所示:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:orientation="vertical"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"
>

<TableLayout 
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+id/columnheadings"
 android:layout_height="wrap_content" 
 android:layout_width="fill_parent"/>        

<ListView
    android:id="@+id/android:list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:dividerHeight="2px"
    />
    ...     
</LinearLayout>

创建活动后,我可以成功地将 TableRow 插入 TableLayout “columnheadings”:

 table = (TableLayout)findViewById(R.id.columnheadings);
 table.removeAllViews();
 TableRow tr = new TableRow(this);
 TextView tv = new TextView(activity);
 tv.setText("asdf");
 tr.addView(tv);
 table.addView(tr);
 table.postInvalidate();

表格列的内容(和宽度)是动态计算的。

现在我还想重绘 TableLayout,当 ListView 的 Adapter 中有更新时。为此,我正在创建带有对活动的引用的适配器。

从适配器中,我可以成功地更改例如列的文本。

我尝试做的事情(没有成功):

 TableLayout tableLayout = (TableLayout)activity.findViewById(android.app.R.id.columnheadings);         

 tableLayout.removeAllViews();
 TableRow row=new TableRow(activity);
 TextView tv = new TextView(activity);
 tv.setText("test");

 row.addView(tv);
 tableLayout.addView(row);

 tableLayout.postInvalidate();

不幸的是,唯一发生的事情是删除当前表行。新行不会从适配器添加到 tableLayout。

有什么建议么 ?

4

1 回答 1

0

我不记得为什么,但我的代码row.getParent().requestLayout();在所有操作结束时都有。似乎没有它对我也不起作用。我不需要TableLayout在我的代码中引用,这就是我使用的原因row.getParent(),我认为你可以简单地编写tableLayout.requestLayout();它,它会有所帮助。

于 2011-01-04T21:03:45.690 回答