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

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6


我需要制作一个图形插件,需要用到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与代码的衔接貌似有问题,很难当做同一个过程来看待。哎!!

 

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6

“The specified child already has a parent. You must call removeView() on the chil” 的相关文章