Android基础:三种inflate的区别
来源:互联网 发布:socket多线程编程 编辑:程序博客网 时间:2024/06/06 03:29
inflate的3种方式
View.inflate(…)
inflater.inflate(…)
LayoutInflater.from(getActivity()).inflate(…)
实例: 类:MenuFragment
@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { view = inflater.inflate(R.layout.layout_menu, null);//正确// view = inflater.inflate(R.layout.layout_menu, container);//错误// view = inflater.inflate(R.layout.layout_menu, container, false);//正确// view = inflater.inflate(R.layout.layout_menu, container, true);//错误// view = View.inflate(getActivity(), R.layout.layout_menu, null);//正确 //跟参数LayoutInflater inflater一样// view = LayoutInflater.from(getActivity()).inflate(R.layout.layout_menu, null);//正确// view = LayoutInflater.from(getActivity()).inflate(R.layout.layout_menu, container);//错误 return view;}
错误:
Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child’s parent first.
截图:
原因:
创建fragment的时候,会自动给view添加parent,如果我们还用container的话,就会有2个parent,所以报错,所以我们就不需要用container。
解决方法:
不用container,直接填null。
如若用container的话,那么第三个参数必须是false,表示该view不与此parent绑定。
0 0
- Android基础:三种inflate的区别
- Android基础:三种inflate的区别
- 关于Android inflate三种参数的区别
- Android inflate的三种使用方法
- LayoutInflater方法Inflate三种重载的区别
- layoutinflater的三种inflate用法
- View.inflate()与LayoutInflater.inflate()的区别?
- [Android]inflate方法与 findViewById 方法区别 | LayoutInflater的inflate函数用法详解
- [Android]inflate方法与 findViewById 方法区别 | LayoutInflater的inflate函数用法详解
- Android setContentView和inflate区别
- 【Android】【Inflate】inflate方法的三个参数
- android 中findViewById和inflate的使用区别
- android Inflate 的作用
- android的inflate方法
- android的inflate
- Android Inflate 的使用
- Android的inflate()方法
- setContentView()和inflate()的区别
- 单点触控
- CSU 1775 悲催的移寝 贪心
- cmd下输入python,报不是内部或外部命令错误的解决方法
- 欢迎使用CSDN-markdown编辑器
- 字符串拷贝函数strcpy()
- Android基础:三种inflate的区别
- iOS之 多线程中的NSOperation
- 使用 Docker 搭建 Java Web 运行环境
- 用UIWebView打开文档(doc、xls、pdf等)
- CSU 1765 简单shell 模拟
- 小练习——输出9*9乘法表,判断闰年
- Codeforces546B Soldier and Badges
- Java线程安全和线程不安全理解
- KMeans聚类算法Hadoop实现