美文网首页
Android几种inflate的方式

Android几种inflate的方式

作者: vicedev | 来源:发表于2018-03-25 16:25 被阅读0次

    前言

    Android中几种inflate的方法大家可能都已经很熟悉了,但是这几种方式到底有什么区别,让我们一起来看下。

    方式一:

    View.inflate(Context context, @LayoutRes int resource, ViewGroup root)
    

    方式二:

    LayoutInflater inflater = LayoutInflater.from(Context context);
    inflater.inflate(@LayoutRes int resource, @Nullable ViewGroup root)
    

    方式三:

    LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    inflater.inflate(@LayoutRes int resource, @Nullable ViewGroup root)
    

    方式四:(Activity中)

    LayoutInflater inflater = getLayoutInflater();
    inflater.inflate(@LayoutRes int resource, @Nullable ViewGroup root)
    

    先让我们看下几种获取LayoutInflater的方式的区别

        public static View inflate(Context context, @LayoutRes int resource, ViewGroup root) {
            LayoutInflater factory = LayoutInflater.from(context);
            return factory.inflate(resource, root);
        }
    

    可以看到在View的inflate方法中直接就调用了LayoutInflater.from(Context context)的方式生成LayoutInflater,
    那么LayoutInflater.from(Context context)做了什么。

        public static LayoutInflater from(Context context) {
            LayoutInflater LayoutInflater =
                    (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            if (LayoutInflater == null) {
                throw new AssertionError("LayoutInflater not found.");
            }
            return LayoutInflater;
        }
    

    结果发现就是方式三,是不是有种被欺骗的感觉...好吧,那么第四种是怎么回事呢。

        public LayoutInflater getLayoutInflater() {
            return getWindow().getLayoutInflater();
        }
    

    调用了Window的getLayoutInflater方法,那么看下这个Window是什么东西。

        public Window getWindow() {
            return mWindow;
        }
    
    final void attach(Context context, ActivityThread aThread,
                Instrumentation instr, IBinder token, int ident,
                Application application, Intent intent, ActivityInfo info,
                CharSequence title, Activity parent, String id,
                NonConfigurationInstances lastNonConfigurationInstances,
                Configuration config, String referrer, IVoiceInteractor voiceInteractor,
                Window window, ActivityConfigCallback activityConfigCallback) {
            ...
            ...
            mWindow = new PhoneWindow(this, window, activityConfigCallback);
            ...
            ...
        }
    

    可以看到在Activity attach方法中mWindow指向了PhoneWindow,看下PhoneWindow的getLayoutInflater方法。

        @Override
        public LayoutInflater getLayoutInflater() {
            return mLayoutInflater;
        }
    
        public PhoneWindow(Context context) {
            super(context);
            mLayoutInflater = LayoutInflater.from(context);
        }
    

    最终还是调用了LayoutInflater.from(context)方法,又陷入了循环中...所以这几种方式获取的LayoutInflater是一样的,既然是一样的,我们下面就直接看它的inflate方法,传不同的参数会有什么不同。

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

    可以看到两个参数的方法会调用三个参数的方法,attachToRoot 的值为:root != null,我们直接看 return inflate(parser, root, attachToRoot);方法做了什么。

        public View inflate(XmlPullParser parser, @Nullable ViewGroup root, boolean attachToRoot) {
          ...
          ...
                  View result = root;
          ...
          ...
                        // 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);
                            }
                        }
          ...
          ...
                        // 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;
                        }
          ...
          ...
                  return result;
        }
    

    createViewFromTag方法就是吧layout布局变成了view,但是另外两个参数会影响最终生成的view,当root != null并且
    attachToRoot为false的时候,可以看到view调用了setLayoutParams方法,而这个params是通过root.generateLayoutParams获取到的,也就是说root的LayoutParams会影响view,root != null && attachToRoot的时候把view添加到了root里面,这就好解释为什么上面要给view设置root的LayoutParams了,我们直接在java代码中创建控件的时候,给它设置LayoutParams时都要考虑它的父布局是什么类型,比如LinearLayout就要创建LinearLayout.LayoutParams。

    总结

    几种方式最终生成view的状态与root和attachToRoot有关:
    root!=null时:
        attachToRoot==true:
        最终返回root,layout生成的view会被添加到root中。
        attachToRoot==false:
        最终返回layout生成的view,但是view会被root的LayoutParams影响。
    root==null时:
        最终返回layout生成的view,不会被其他东西影响。

    相关文章

      网友评论

          本文标题:Android几种inflate的方式

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