从代码中替换表格行时遇到了一个奇怪的问题。
我的 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。
有什么建议么 ?