我正在动态构建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.
ERROR / AndroidRuntime(771):引起:java.lang.IndexOutOfBoundsException:index = 2 count = 0
任何建议,将不胜感激.
谢谢.
最佳答案
实际上,乍一看你的代码错误似乎很合乎逻辑.除非您是从xml创建的表并且是所需的行数,否则在索引2处添加视图时,此索引不存在.尝试将此2替换为0,您将看到它有效.因此,如果您想坚持使用其他索引,您只需要添加空ImageView.