Inflate()方法详解(1)

1,016 阅读4分钟

作者:不洗碗工作室 - XingDingWei

版权归作者所有,转载请注明出处

先说比较常用的这个inflate方法,就是直接使用View进行调用这个方法,把View的实例化对象和xml Source文件进行绑定,通过看此方法的源码,源码中的说明是这样的

/**
 * Inflate a view from an XML resource.  This convenience method 
 * wraps the {@linkLayoutInflater} 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 thelayout_* parameters.
 * @see LayoutInflater
 */

这个方法是封装了linkLayoutInflater这个类中的方法进行使用的,有三个参数:context和xmlSource的id不用说,主要是第三个参数的含义,单看对于第三个参数的解释,实在是看不出什么东西,而且一般用这个方法时inflate的第三个参数是设置成null的。既然文档看不懂,那么就看一下方法内部的实现原理,看一下在哪里用到了这个参数,再去确定它的含义。

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

方法内部就两行代码,就是实例化一个LayoutInflater的对象,然后返回用这个对象调用一个inflate的方法,进入这个方法,先看方法的说明:

 /**
 * 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.
 */

这个方法和之前的那个方法从说明上面来看区别不大,即从指定的xml文件生成新的view视图关系。出现错误时,抛出InflateException异常。然后看参数,第一个是xml文件的id,第二个参数就是我们关心的root参数,翻译了一下,还是不怎么能理解它的意思,然后看返回值:返回根View的层次,如果有根View,返回根View,否则根就是xml文件的id,看到这里,还是有点懵的,因为我不知道它的root所指的是什么,那就继续往里面看:

public View inflate(@LayoutRes int resource, @Nullable ViewGroup root) {
    return inflate(resource, root, root != null);
}

这是它的方法,进入它return的方法,看它的说明是这样的:

/**
 * 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.
 */

这个方法的作用和上一个差不多,然后看参数说明:第一个是xml文件的id,第二个是root,看起来和第三个参数有关系,第三个参数的注释是这样的:是否应该将inflate的层次附加到根参数?如果这个参数为false,root只用于在XML的根视图创建正确的LayoutParams。似乎还是有点迷,但是看前一个方法中关于这个方法的调用,第三个参数是root!=null,也就是说如果root是null,第三个参数就是false,也就是说root仅仅被用在创建correct subclass of LayoutParams for the root view in the XML这个上面。下面看一下方法的内容:

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

这个方法中的内容,现阶段能力有限,待后期解读,关于上面的部分,可以解读到这样的含义:

root是填充的根视图,一般将root参数置空,虽然可以使用inflate,但是会有隐患,如果把root置空,根布局(最外层布局)的height等属性设置会不起作用,如果把root参数的位置置为false,根据上文的代码,可以看出当root传为false的时候,会把根控件的属性添加到View中,使成为View的属性。也就是说,如果想不改变inflate绑定的视图,要把root赋值false。

那么,暂时对于inflate方法的解读就到这里,后期编者会对inflate的方法进行进一步的解读,同时加上对于XmlPullParser和InflateException的理解,敬请期待。