对LayoutInflater的简单理解

来源:互联网 发布:淘宝怎么转接人工服务 编辑:程序博客网 时间:2024/05/18 02:44

LayoutInflater的获得实例的三种方法

1.LayoutInflater inflater = getLayoutInflater();  //调用Activity的getLayoutInflater()2.LayoutInflater localinflater =(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);3. LayoutInflater inflater = LayoutInflater.from(context);  

众所周知,inflater.inflate的三种加载模式

1.inflate(layoutId, null );2.inflate(layoutId, root, false );3.inflate(layoutId, root, true )

通过观察下面的源码我们可以知道,1和2是进入了第二个条件即没有附带参数,即你在xml写的参数是不会起作用的,要想参数起作用就只有用第3的一个方法。

// 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;}

其次对于1,2也是有区别的,1是直接加载出一个组件,2是加载出一个组件后还要将组件放到root布局里面去,最后返回的视图就是组件加入后的视图。所以1,2就必须要动态填入参数,wrap_content或match_parent。1还需要addView()将他加入父布局如下图:
这里写图片描述
最后福利一个非常有用的链接:http://blog.csdn.net/yanbober/article/details/45970721

0 0
原创粉丝点击