您的位置:首页 > 其它

对LayoutInflater的简单理解

2016-05-09 14:37 465 查看
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()将他加入父布局如下图:



最后福利一个非常有用的链接:/article/1503765.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: