美文网首页
LayoutInflater源码分析

LayoutInflater源码分析

作者: zsj1225 | 来源:发表于2018-06-23 16:21 被阅读11次
    LayoutInflater.from(mContext).inflate(mLayoutId,parent,false);
    

    1.1 LayoutInflater.from(mContext)源码分析

        /**
         * Obtains the LayoutInflater from the given 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;
        }
    
         //获取系统服务
        @Override
        public Object getSystemService(String name) {
            return SystemServiceRegistry.getSystemService(this, name);
        }
    
        // Service registry information.
        // This information is never changed once static initialization has completed.
        private static final HashMap<Class<?>, String> SYSTEM_SERVICE_NAMES =
                new HashMap<Class<?>, String>();
        //在静态代码块中注册LAYOUT_INFLATER_SERVICE的服务.单例
        static {
                registerService(Context.LAYOUT_INFLATER_SERVICE, LayoutInflater.class,
                    new CachedServiceFetcher<LayoutInflater>() {
                @Override
                public LayoutInflater createService(ContextImpl ctx) {
                    return new PhoneLayoutInflater(ctx.getOuterContext());
                }});
        }
    
        /**
         * Gets the name of the system-level service that is represented by the specified class.
         */
        public static String getSystemServiceName(Class<?> serviceClass) {
            //从静态的Map获取系统服务.
            return SYSTEM_SERVICE_NAMES.get(serviceClass);
        }
    

    所以LayoutInflater.from(mContext)是一个系统服务.并且是单例的.

    1.2 inflate源码分析

        public View inflate(@LayoutRes int resource, @Nullable ViewGroup root, boolean attachToRoot) {
            //创建Xml解析器
            final XmlResourceParser parser = res.getLayout(resource);
            try {
                //调用这个方法
                return inflate(parser, root, attachToRoot);
            } finally {
                parser.close();
            }
        }
    
        public View inflate(XmlPullParser parser, @Nullable ViewGroup root, boolean attachToRoot) {
         // Temp is the root view that was found in the xml
         //根据name实例化View
         final View temp = createViewFromTag(root, name, inflaterContext, attrs);
        }
    
        private View createViewFromTag(View parent, String name, Context context, AttributeSet attrs) {
            return createViewFromTag(parent, name, context, attrs, false);
        }
    
        View createViewFromTag(View parent, String name, Context context, AttributeSet attrs,
                boolean ignoreThemeAttr) {
     View view;
                //前提看你有没有设置Factory.其实在 AppCompatActivity里面会走这个方法,也就会去替换某些控件
                if (mFactory2 != null) {
                    view = mFactory2.onCreateView(parent, name, context, attrs);
                } else if (mFactory != null) {
                    view = mFactory.onCreateView(name, context, attrs);
                } else {
                    view = null;
                }
    
                if (view == null) {
                    final Object lastContext = mConstructorArgs[0];
                    mConstructorArgs[0] = context;
                    try {
                        if (-1 == name.indexOf('.')) {
                            //系统的View.在xml中写不带 . 的
                            view = onCreateView(parent, name, attrs);
                        } else {
                            view = createView(name, null, attrs);
                        }
                    } finally {
                        mConstructorArgs[0] = lastContext;
                    }
                }
    }
    
        protected View onCreateView(View parent, String name, AttributeSet attrs)
                throws ClassNotFoundException {
            return onCreateView(name, attrs);
        }
    
        protected View onCreateView(String name, AttributeSet attrs)
                throws ClassNotFoundException {
            return createView(name, "android.view.", attrs);
        }
        
        //通过反射创建View
        public final View createView(String name, String prefix, AttributeSet attrs)
                throws ClassNotFoundException, InflateException {
        }
    

    相关文章

      网友评论

          本文标题:LayoutInflater源码分析

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