美文网首页Java、Android技术栈
LayoutInflater 填充器解析

LayoutInflater 填充器解析

作者: 魏树鑫 | 来源:发表于2019-04-13 16:59 被阅读1次

    填充View最终都是调用LayoutInflaterpublic View inflate(@LayoutRes int resource, @Nullable ViewGroup root, boolean attachToRoot)三个参数方法

        /**
         * Inflate a new view hierarchy from the specified xml resource. Throws
         * {@link InflateException} if there is an error.
         *
         * @param resource ID for an XML layout resource to load (e.g.,
         *        <code>R.layout.main_page</code>)
         * @param root Optional view to be the parent of the generated hierarchy (if
         *        <em>attachToRoot</em> is true), or else simply an object that
         *        provides a set of LayoutParams values for root of the returned
         *        hierarchy (if <em>attachToRoot</em> is false.)
         * @param attachToRoot Whether the inflated hierarchy should be attached to
         *        the root parameter? If false, root is only used to create the
         *        correct subclass of LayoutParams for the root view in the XML.
         * @return The root View of the inflated hierarchy. If root was supplied and
         *         attachToRoot is true, this is root; otherwise it is the root of
         *         the inflated XML file.
         */
        public View inflate(@LayoutRes int resource, @Nullable ViewGroup root, boolean attachToRoot){
               ··· ···
            return view;
        }
    

    文档解释说明:

         /**
         * 只能填充特定格式的xml也就是得符合布局格式的xml,否则抛出异常InflateException。
         * @param resource 要填充的资源id
         * @param root 这个要和第三个参数有关系。
         *             若是attachToRoot为true,那么root的意义是,从resource填充成的view对象的父控件;
         *             若是attachToRoot为false,那么root的意义是,可以为resource生成的view对象的根布局提供一系LayoutParams参数的控件。
         *             若root为null,也就是我们不指定父控件,那么新生产的view对象的根布局的某些参数会失效,比如Layout_width和Layout_height会失效。
         * @param attachToRoot 是否附着在root上
         * @return 如果attachToRoo为false,那么返回resource填充的View;
         *         如果attachToRoo为true,那么返回root。
         */
        public View inflate(@LayoutRes int resource, @Nullable ViewGroup root, boolean attachToRoot){
               ··· ···
            return view;
        }
    

    常用填充View的方式有三种:

    1. View的静态方法,直接获取LayoutInflater进行填充
         /**
         * Inflate a view from an XML resource.  This convenience method wraps the {@link
         * LayoutInflater} class, which provides a full range of options for view inflation.
         *
         * @param context The Context object for your activity or application.
         * @param resource The resource ID to inflate
         * @param root A view group that will be the parent.  Used to properly inflate the
         * layout_* parameters.
         * @see LayoutInflater
         */
        public static View inflate(Context context, @LayoutRes int resource, ViewGroup root) {
            LayoutInflater factory = LayoutInflater.from(context);
            return factory.inflate(resource, root);
        }
    
    1. LayoutInflater的静态方法获取LayoutInflater,然后调用Inflater填充
         /**
         * 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;
        }
    
    1. 通过系统SystemService获取填充器,然后调用Inflater填充
    LayoutInflater LayoutInflater = 
     (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)
    

    注意:

    View的填充方式只有两个参数,也就是调用的inflate(resource, root, root != null)
    而RecyclerViewonCreateViewHolder创建View填充时一定是inflate(resource, null)orinflate(resource, root, false)
    root为空时view的根节点部分参数无效
    否则会报

    image.png

    相关文章

      网友评论

        本文标题:LayoutInflater 填充器解析

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