美文网首页程序员
简析LayoutInflate工作流程

简析LayoutInflate工作流程

作者: 奔跑之咸鱼 | 来源:发表于2018-04-28 16:10 被阅读0次

    看完郭神的解析后,我打算做一些简单的总结。一开始了解到的东西是LayoutInflate,既然是源码分析,那就得先能查看源码,查看源码的方法是打开你的Android Studio点击File -》Setting -》搜索框中搜索Android SDK -》Show Package Details -》下载source for Android 23(因为我手机是安卓6.0版本我就习惯性的下了6.0的源码,最好还是下最新版本的源码)

    下载源码

    先来研究的东西是LayoutInflate:

    不管你用哪个inflate方法的重载你都会转到inflate(XmlPullParser parser, ViewGroup root, boolean attachToRoot)中

    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!");

    }

    final String name = parser.getName();

    if (DEBUG) {

    System.out.println("**************************");

    System.out.println("Creating root view: "

                            + name);

    System.out.println("**************************");

    }

    if (TAG_MERGE.equals(name)) {

    if (root ==null || !attachToRoot) {

    throw new InflateException(" can be used only with a valid "

                                +"ViewGroup root and attachToRoot=true");

    }

    rInflate(parser, root, inflaterContext, attrs,false);

    }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) {

    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);

    }

    }

    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.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;

    }

    }

    }catch (XmlPullParserException e) {

    final InflateException ie =new InflateException(e.getMessage(), e);

    ie.setStackTrace(EMPTY_STACK_TRACE);

    throw ie;

    }catch (Exception e) {

    final InflateException ie =new InflateException(parser.getPositionDescription()

    +": " + e.getMessage(), e);

    ie.setStackTrace(EMPTY_STACK_TRACE);

    throw ie;

    }finally {

    // Don't retain static reference on context.

                mConstructorArgs[0] = lastContext;

    mConstructorArgs[1] =null;

    Trace.traceEnd(Trace.TRACE_TAG_VIEW);

    }

    return result;

    }

    }

    可以看到他先用pull解析方式来解析xml文件,再通过createViewFromTag()把参数传给他,你要是在点进去查看该方法你就可以知道它是用createView()来创建并返回一个View的实例的,再通过rInflate()查找view下的子元素并逐一添加进父布局中,随着版本更新迭代,源码中多了一个rInflateChildren(),实际上他也调用了rInflate(),这样做的目的是填充所有子布局。

    我们在调用setContentView()时,Android会自动在布局文件的最外层再嵌套一个帧布局,如果没有外面套着的这一层布局就无法设置该布局的width和height

    相关文章

      网友评论

        本文标题:简析LayoutInflate工作流程

        本文链接:https://www.haomeiwen.com/subject/ehuglftx.html