5

我正在动态构建 TableLayout。而且我需要 TableRow 在某些列位置有间隙。

例如,我需要行在 3 和 5 位置有 ImageView,下一行在 1、2、4 位置有 ImageView。我尝试使用:

   TableRow row1 = new TableRow(this);  
   row1.addView(new ImageView(this), 2);  
   row1.addView(new ImageView(this), 4);  
   mTable.addView(row1);
   TableRow row2 = new TableRow(this);  
   row2.addView(new ImageView(this), 0);  
   row2.addView(new ImageView(this), 1);  
   row2.addView(new ImageView(this), 3);
   mTable.addView(row2);

但我在运行时得到了 IndexOfBoundException 。
错误/AndroidRuntime(771): 原因: java.lang.IndexOutOfBoundsException: index=2 count=0

任何建议,将不胜感激。
谢谢你。

4

1 回答 1

4

实际上,乍一看您的代码,该错误似乎很合乎逻辑。除非您的表是从 xml 创建的并且作为所需的行数,否则当您在索引 2 处添加视图时,此索引不存在。尝试将此 2 替换为 0,您会发现它有效。所以如果你想坚持你的索引,你只需要添加空的 ImageView 其他索引。

于 2010-07-27T15:47:45.410 回答