Android编程之LayoutInflater的inflate方法详解
来源:互联网 发布:张爱玲老公 知乎 编辑:程序博客网 时间:2024/06/14 14:52
LayoutInflater的inflate方法,在fragment的onCreateView方法中经常用到:
- public View onCreateView(LayoutInflater inflater, ViewGroup container,
- Bundle savedInstanceState) {
LayoutInflater的inflate方法一共有四种,但我们日常用经常用到的就只有这两种:
- public View inflate(int resource, ViewGroup root) {
- return inflate(resource, root, root != null);
- }
- public View inflate(int resource, ViewGroup root, boolean attachToRoot) {
- if (DEBUG) System.out.println("INFLATING from resource: " + resource);
- XmlResourceParser parser = getContext().getResources().getLayout(resource);
- try {
- return inflate(parser, root, attachToRoot);
- } finally {
- parser.close();
- }
- }
所以,这里直接介绍里面调用这个方法即可:
- public View inflate(XmlPullParser parser, ViewGroup root, boolean attachToRoot)
在这个方法里面,上半部分为xml解析的代码,这里就不贴出来,确实没什么东西可看。直接看中间部分的代码:
- ViewGroup.LayoutParams params = null;
- if (root != null) {
- if (DEBUG) {
- System.out.println("Creating params from root: " +
- root);
- }
- // Create layout params that match root, if supplied
- params = root.generateLayoutParams(attrs);
- if (!attachToRoot) {
- // Set the layout params for temp if we are not
- // attaching. (If we are, we use addView, below)
- temp.setLayoutParams(params);
- }
- }
params = root.generateLayoutParams(attrs);
这段的意思是:如果调用inflate方法,传入了ViewGroup root参数,则会从root中得到由layout_width和layout_height组成的LayoutParams,在attachToRoot设置为false的话,就会对我们加载的视图View设置该LayoutParams。
接着往下看:
- // We are supposed to attach all the views we found (int temp)
- // to root. Do that now.
- if (root != null && attachToRoot) {
- root.addView(temp, params);
- }
- // Decide whether to return the root that was passed in or the
- // top view found in xml.
- if (root == null || !attachToRoot) {
- result = temp;
- }
root.addView(temp, params);
如果设置了ViewGroup root参数,且attachToRoot设置为true的话,则将我们加载的视图做为子视图添加到root视图中。
如果我们ViewGroup root设置为空的话,就直接返回我们创建的视图;如果root不为空,且attachToRoot设置为false的话,就返回上面那段:对我们加载的视图View设置该LayoutParams。
以上就是该方法内容,可能你还有点看不太懂吧,下一篇文章,我将会做几个例子,来具体说明一下。
0 0
- Android编程之LayoutInflater的inflate方法详解
- Android编程之LayoutInflater的inflate方法详解
- Android编程之LayoutInflater的inflate方法详解
- Android编程之LayoutInflater的inflate方法详解
- Android编程之LayoutInflater的inflate方法示例详解(超赞哦)
- Android编程之LayoutInflater的inflate方法详解
- Android编程之LayoutInflater的inflate方法详解
- Android编程之LayoutInflater的inflate方法详解
- Android编程之LayoutInflater的inflate方法详解
- Android编程之LayoutInflater的inflate方法详解
- Android编程之LayoutInflater的inflate方法详解
- Android编程之LayoutInflater的inflate方法
- Android编程之LayoutInflater的inflate方法详解及其中参数的作用
- Android编程之LayoutInflater的inflate方法实例
- Android编程之LayoutInflater的inflate方法实例
- Android编程之LayoutInflater的inflate方法实例
- Android编程之LayoutInflater的inflate方法实例
- Android编程之LayoutInflater的inflate方法实例
- Hibernate缓存问题
- 【慕课笔记】2-2 数据类型与操作数据表—数据类型之整形
- iOS 集成银联支付
- PowerDesigner表创建脚本双引号问题
- hdu 2874(LCA + 节点间距离)
- Android编程之LayoutInflater的inflate方法详解
- JVM调优总结:一些概念
- JAVA 线程同步
- (p226)最长单调递增子序列
- 贪心1006
- tomcat性能调优
- 分析JAVA应用CPU占用过高的问题
- 解决MX4无法打印LOG
- iOS开发证书变成“此证书的签发者无效”解决方法