我动态构建TableLayout。而我需要的TableRow具有一定的列位置的差距。
I'm constructing TableLayout dynamically. And I need TableRow has a gap in certain column position.
例如,我需要行有ImageView的3和5位,下一行有ImageView的1,2,4位。我尝试使用:
For example, I need row has ImageView on 3 and 5 position, next row has ImageView on 1, 2, 4 position. I try to use:
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:产生的原因指数= 2计数= 0
but I got IndexOfBoundException at Runtime. ERROR/AndroidRuntime(771): Caused by: java.lang.IndexOutOfBoundsException: index=2 count=0
任何建议将是AP preciated。 谢谢你。
Any suggestions would be appreciated. Thank you.
其实,错误似乎很合乎逻辑的,在你的code乍一看。除非你的表是从XML和为行所需数量的,当你的索引2处添加视图创建的,该指数不存在。尝试用0代替该2,你会看到它的工作原理。所以,你只需要添加空的ImageView其他指标,如果你要坚持你的。
Actually, the error seems quite logical at first glance on your code. Unless you table is created from xml and as the required number of rows, when you add a view at the index 2, this index does not exist. Try replacing this 2 by a 0, you will see that it works. So you just need to add empty ImageView the other indexes if you want to stick to yours.