【问题标题】:Android changing tableLayout from code doesn't workAndroid 从代码更改 tableLayout 不起作用
【发布时间】:2011-03-16 20:29:05
【问题描述】:

我在用代码替换表格行时遇到了一个奇怪的问题。

我的 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>

创建activity后,我可以成功地将一个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。

有什么建议吗?

【问题讨论】:

    标签: android listview tablelayout


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-06
      • 1970-01-01
      相关资源
      最近更新 更多