博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android编程之LayoutInflater的inflate方法具体解释
阅读量:4605 次
发布时间:2019-06-09

本文共 2529 字,大约阅读时间需要 8 分钟。

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。

以上就是该方法内容,可能你还有点看不太懂吧,下一篇文章,我将会做几个样例,来详细说明一下。

转载于:https://www.cnblogs.com/zfyouxi/p/4048032.html

你可能感兴趣的文章
asp.net core学习
查看>>
LeetCode:165. 比较版本号
查看>>
JavaScript丨数组元素反转
查看>>
一维数组计算最大子数组并且能进行步骤可视化
查看>>
C#语法——委托,架构的血液
查看>>
shell 递归变量改变问题
查看>>
Android ActionBar的基本用法
查看>>
小程序camera组件ios上出现授权的问题
查看>>
C# 线程问题
查看>>
JAVA二分搜索树
查看>>
JAVA线程优先级
查看>>
解决VC几个编译问题的方法——好用
查看>>
SPOJ #11 Factorial
查看>>
City Upgrades
查看>>
order set 有序集合
查看>>
“人少也能办大事”---K2 BPM老客户交流会
查看>>
关于七牛进行图片添加文字水印操作小计
查看>>
DataSource数据库的使用
查看>>
CentOS开启samba实现文件共享
查看>>
MSSQL使用sqlbulkcopy批量插入数据
查看>>