LayoutInflater的作用
主要是用来加载布局的。广泛用于动态添加View布局。
相关点
- 通过在Activity中,setContentView()方法也是用来加载布局的,其实setContentView的内部也是使用LayoutInflater来加载布局的
- 与findViewById是什么关系?findViewById()是找xml布局文件下具体的widget控件。LayoutInflater是用来找到布局文件,并将它实例化。通过实例化来findViewById找到具体的界面元素。
获取LayoutInflater实例的三种方式
- LayoutInflater layoutInflater=getLayoutInflater();调用ActivityLayoutInflater实例
- LayoutInflater layoutInflater=LayoutInflater.from(context);
- LayoutInflater layoutInflater(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
三者的关系是什么呢?1、2的最终实现都是使用了3的形式。所以可以说是第3种方式是最基本的实现。
LayoutInflate的inflate()几种重载方法
public View inflate(int resource,ViewGroup root)
public View More ...inflate(int resource, ViewGroup root) {
return inflate(resource, root, root != null);
}
public View inflate(int resource,ViewGroup root,boolean attachToRoot)
public View More ...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)
public View More ...inflate(XmlPullParser parser, ViewGroup root) {
return inflate(parser, root, root != null);
}
public View inflate(XmlPullParser parser,ViewGroup root,boolean attachToRoot)
可以看出上面的三种方法中最后调用的都是方法四的方式加载,而我们最经常用到的就是方法2,结合方法四的代码,我们来分析一下三个方法参数所代表的含义
public View inflate(XmlPullParser parser, @Nullable ViewGroup root, boolean attachToRoot) {
synchronized (mConstructorArgs) {
Trace.traceBegin(Trace.TRACE_TAG_VIEW, "inflate");
final Context inflaterContext = mContext;
//返回每个节点或者标签的属性值集合
final AttributeSet attrs = Xml.asAttributeSet(parser);
Context lastContext = (Context) mConstructorArgs[0];
mConstructorArgs[0] = inflaterContext;
View result = root;
try {
// Look for the root node.
int type;
while ((type = parser.next()) != XmlPullParser.START_TAG &&
type != XmlPullParser.END_DOCUMENT) {
// Empty
}
if (type != XmlPullParser.START_TAG) {
throw new InflateException(parser.getPositionDescription()
+ ": No start tag found!");
}
//type等于root节点
final String name = parser.getName();
Log.d("node",name);
..................
if (TAG_MERGE.equals(name)) {//如果布局的根节点是merge的话
...............
} else {//一般会执行这里
// Temp is the root view that was found in the xml
final View temp = createViewFromTag(root, name, inflaterContext, attrs);
ViewGroup.LayoutParams params = null;
if (root != null) {//注意这里,如果root不为空,生成root的布局参数,但是此时并没有生效
if (DEBUG) {
System.out.println("Creating params from root: " +
root);
}
// Create layout params that match root, if supplied
params = root.generateLayoutParams(attrs);
if (!attachToRoot) {//当attachToRoot为false时,对temp设置布局,还是没有生效
// Set the layout params for temp if we are not
// attaching. (If we are, we use addView, below)
temp.setLayoutParams(params);
}
}
if (DEBUG) {
System.out.println("-----> start inflating children");
}
// Inflate all children under temp against its context.
rInflateChildren(parser, temp, attrs, true);
if (DEBUG) {
System.out.println("-----> done inflating children");
}
// We are supposed to attach all the views we found (int temp)
// to root. Do that now.
if (root != null && attachToRoot) {//root不为null,且attachToRoot为true.root布局生效
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) {当root为null或attachToRoot为false时,可以返回temp(即为子View,resource生成的布局)
result = temp;
}
}
} catch (XmlPullParserException e) {
InflateException ex = new InflateException(e.getMessage());
ex.initCause(e);
throw ex;
} catch (Exception e) {
InflateException ex = new InflateException(
parser.getPositionDescription()
+ ": " + e.getMessage());
ex.initCause(e);
throw ex;
} finally {
// Don't retain static reference on context.
mConstructorArgs[0] = lastContext;
mConstructorArgs[1] = null;
}
Trace.traceEnd(Trace.TRACE_TAG_VIEW);
return result;
}
}
我们结合inflate(int resource, ViewGroup root, boolean attachToRoot)方式,对其中的参数设置做一下说明。
1、resource 是我们要加载的布局
2、root 是resource布局的父布局,即resource布局要放入的布局
3、attachToRoot是将resource的实例化的布局放入root中
有这几种情况需要分别说明一下。
inflate(resource,root,false) resource的布局没有添加到root中,返回的结果resource的布局
inflate(resource,root,true) resource的布局添加到root布局中,返回的结果是root布局
inflate(resource,null,false) 返回的是resource的布局
http://stackoverflow.com/questions/5026926/making-sense-of-layoutinflater/5027921#5027921
这里的例子可以得出各个布局的大小。
http://blog.csdn.net/lmj623565791/article/details/38171465
上面的两个链接结合,就能够搞清楚LayoutInflater的种种问题了。