代码片段,双击复制
01
02
03
04
|
for
(
int
i=
0
; i <
5
; i++){
View view = LayoutInflater.from(
this
).inflate(R.layout.item_bg,
null
);
lin_item.addView(view);
}
|
lin_item为一个LinearLayout,把view动态加到这个lin_item中,但是只能加进一条数据。。。要不然就报07-28 22:03:49.360: ERROR/AndroidRuntime(1403): Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first. 这个异常,我知道view为同一个,所以会产生这种异常。但是我就是要这样动态的加入view到lin_item里啊。。有没什么办法呢。。
代码片段,双击复制
01
02
03
04
05
06
|
View view1 = LayoutInflater.from(
this
).inflate(R.layout.lin_item_ev,
null
);
View view2 = LayoutInflater.from(
this
).inflate(R.layout.lin_item_ev,
null
);
View view3 = LayoutInflater.from(
this
).inflate(R.layout.lin_item_ev,
null
);
lin_item.addView(view1);
lin_item.addView(view2);
lin_item.addView(view3);
|
如果这样写就没问题了。。但是还是同一个layout啊。。。我知道原因的,但有没办法可以像上面那样动态的生成view并加到linearLayout里。。。 以上同样情况。。同一个view不能让父view addview 2次,所以解决办法是:
LinearLayout ll = (LinearLayout) findViewById(R.id.ll);
dots = new ImageView[imageColors1.length];
// 循环取得小点图片
for (int i = 0; i < imageColors1.length; i++) {
dots[i] = (ImageView) LayoutInflater.from(this).inflate(R.layout.dot, null);
dots[i].setEnabled(true);// 都设为灰色
ll.addView(dots[i]);
}
|