LayoutInflater.inflater()方法就是根据一个xml文件来生成一个view对象,注意跟getviewbyid的区别,后者是从view独享中找到指定的控件。
在LayoutInflater中对inflater()有四种重载分别是
- public View inflate(int, ViewGroup)
- public View inflate(XmlPullParser, ViewGroup)
- public View inflate(int, ViewGroup, boolean)
- public View inflate(XmlPullParser, ViewGroup, boolean)
好的,老规矩上源码,不用看懂,哥们后面会帮你简化的,
public View inflate(@LayoutRes int resource, @Nullable ViewGroup root) {
return inflate(resource, root, root != null);
}
简单来说就是第一个方法调用第三个,好的去找第三个方法
public View inflate(@LayoutRes int resource, @Nullable ViewGroup root, boolean attachToRoot) {
final Resources res = getContext().getResources();
if (DEBUG) {
Log.d(TAG, "INFLATING from resource: \"" + res.getResourceName(resource) + "\" ("
+ Integer.toHexString(resource) + ")");
}
final XmlResourceParser parser = res.getLayout(resource);
try {
return inflate(parser, root, attachToRoot);
} finally {
parser.close();
}
}
得到的答案是根据resource初始化视图后调用4号方法
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)