Android layoutInflate.inflate 方法详解,removeView()错误解决
来源:互联网 发布:手机淘宝店招750x254 编辑:程序博客网 时间:2024/06/05 22:44
错误:
The specified child already has a parent. You must call removeView()。
解答:
这个错误很直白,就是你viewGroup.addView(childView); 中childView已经有父View了。错误原因很多,我主要讲下 mLayoutInflater.inflate(id, rootView, false);造成的这个错误。(该方法有两种,一种是2个参数,一种是3个参数)。
2个参数:
第一个参数:layout的XML文件id;
第二个参数:将XML文件中的根view,作为子View添加到的那个父View。也就是父View布局
a:如果rootView非空,方法返回值是rootView(rootView已添加xml代表的子View);
b:如果rootView为空,方法直接返回子View(xml代表的子View);那么XML文件中的LayoutParams就无效了,(因为LayoutParams是给该XML代表的View的父View使用。在父View的onMeasure()中根据LayoutParams是MATCH_PARENT还是WRAP_CONTENT,来测量子View的大小。如果没有父View就没必要了)
故用mLayoutInflater.inflate(id, rootView);返回的实际上是rootView,而rootView肯定已经有父布局了(DecorateView,系统生成的)。所以 rootView.addView(mLayoutInflater.inflate(id, rootView)).等价于rootView.addView(rootView).所以错了。
3个参数:
问题来了,怎样修复b情况,既保留XML文件中的LayoutParams属性,又不想有方法return父View。
两步 rootView != null (是那个对象随便),第三个参数 == false;mLayoutInflater.inflate(id, rootView, false);这样就可以不attach root view,又保留LayoutParams。
0 0
- Android layoutInflate.inflate 方法详解,removeView()错误解决
- LayoutInflate.inflate()报空指针错误解决
- layoutinflate.inflate方法的使用
- Android LayoutInflate.Inflate(二)
- 关于LayoutInflate.inflate()方法的测试
- Android LayoutInflate.from(...).inflate(...,boolean attachToRoot)方法中attachToRoot参数的意义
- Android:inflate.inflate()方法
- Layoutinflate的inflate用法
- android LayoutInflate.inflate源码分析及使用区分
- [Android]LayoutInflater的inflate方法半详解
- android-----LayoutInflater的inflate方法详解
- Android LayoutInflater.inflate()方法参数详解
- Android LayoutInflate
- LayoutInflater#inflate()方法详解
- [Android]inflate方法与 findViewById 方法区别 | LayoutInflater的inflate函数用法详解
- [Android]inflate方法与 findViewById 方法区别 | LayoutInflater的inflate函数用法详解
- Android之inflate详解
- android LayoutInflater.inflate详解
- 69卸载应用程序
- QML的Mouse事件(MouseArea)详解
- 看看飞比是如何与国际品牌PHILIPS HUE产品实现互联互通
- Git/Github的使用并与Eclipse整合
- 安卓开发-HttpClient使用
- Android layoutInflate.inflate 方法详解,removeView()错误解决
- 第17周项目6-(4)
- 能否重拾Linux下Init 3的快感?
- .Net程序员玩转Android开发---(14)客户端与服务器进行通信
- Android Studio常用快捷键、Android Studio快捷键大全
- 分表时的多维度查询
- Android中pendingIntent的深入理解
- iOS中正确的截屏姿势
- 数据库记录锁表锁实际研究笔记 --- MSSQLSERVER