安卓的TableRow - 如何动态地添加视图某些现在的位置? [英] Android TableRow - How to add View dynamically to certain postion?

查看:389
本文介绍了安卓的TableRow - 如何动态地添加视图某些现在的位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我动态构建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.

这篇关于安卓的TableRow - 如何动态地添加视图某些现在的位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆