LayoutInflater中inflate方法那些事儿

关于这个方法,之前看到别人介绍过,然后就"好读书,不求甚解了",但是每次想到总有些捋不清楚,还是看源码注释吧——Talk is cheap, Show me the code

/**
     *以下是官方的方法注释,两个参数的方法最后还是调用三个参数的方法,
     *root==null,则相当于第三个参数为false,返回值是所加载的布局根元素
     *root!=null,则相当于第三个参数为true ,返回值是root
     * 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.
     * @return The root View of the inflated hierarchy. If root was supplied,
     *         this is the root View; otherwise it is the root of the inflated
     *         XML file.
     */
    public View inflate(@LayoutRes int resource, @Nullable ViewGroup root) {
        return inflate(resource, root, root != null);
    }
    /**
     *还是看注释吧,
     *如果root!=null,attachToRoot=false,则root仅仅为加载的布局提供一个布局参数,
     *比如所加在布局layout_width="300dp" layout_height="500dp",此时300dp和500dp生效
     *如果root!=null,attachToRoot=true,则root不仅为加载的布局提供布局参数,
     *比如所加在布局layout_width="300dp" layout_height="500dp",此时300dp和500dp生效
     *此时还将所加载布局添加到root中
     *如果root==null,不论attachToRoot为true还是false,显示效果都是一样的。
     *当root为null,表示我不需要将第一个参数所指定的布局添加到任何容器中,
     *同时也表示没有任何容器来来协助第一个参数所指定布局的根节点生成布局参数。
     *所以它的根节点的宽高属性会失效
     * 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) {
        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();
        }
    }

参考文章:
三个案例带你看懂LayoutInflater中inflate方法两个参数和三个参数的区别

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容