`

The specified child already has a parent. You must call removeView() on the chil

 
阅读更多

我需要制作一个图形插件,需要用到freamelayout中的叠加功能,屏幕分别设置了两个imageview,设置的layout_gravity为left,true。但是我发现在动态生成imageview时,无法设置其layout_gravity选项,所以考虑用

LayoutInflater inflater = (LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
FrameLayout arrowLayout=(FrameLayout) inflater.inflate(R.layout.arrow, null);
ivr=(ImageView) arrowLayout.findViewById(R.id.xx);

 

方法来加载现成的imageview。但是出现了

 

The specified child already has a parent. You must call removeView() on the child's parent first.

 

错误。

 

原因是:一女不不能二嫁,一个view不能有同级别的两个parent,

所以需要这样:

		ivr=(ImageView) arrowLayout.findViewById(R.id.xx1);
		ivl=(ImageView) arrowLayout.findViewById(R.id.xx2);
		arrowLayout.removeAllViews();
		this.addView(ivr);		
		this.addView(ivl);

在把引用layout上的view取下来后,先再引用layout上删除它们,再加载到本地的layout中就可以了

 

 

 

 

ps:话说回来,android做的布局设置layout.xml与代码的衔接貌似有问题,很难当做同一个过程来看待。哎!!

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics