LayoutInflater的传参问题

Android开发中,我们会经常需要把一个xml解析成一个View。最常见的就是在Adapter的getView方法中通过LayoutInflater把一个布局文件解析成一个View。但是,这里经常会出现一些问题,比如我在item布局文件的根布局中设置的属性经常会没效果等等。其实,在我们在Activity的onCreate方法中调用Activity的setContentView方法给Activity设置布局的时候,底层也是通过LayoutInflater来将传入的布局解析成View然后在添加到界面上的。所以,为了搞清楚LayoutInflater的工作原理,我翻了翻源码,基本上把问题搞明白了。

先来看一个问题:在给ListView填充item布局时,以下几种填充布局的方法有什么异同?

item的布局文件长这样,很简单

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="100dp"
    android:layout_height="50dp"
    android:gravity="center"
    android:background="@android:color/holo_green_light"/>
① convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_list, parent,false);

② convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_list, parent,true);

③ convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_list, null,false);

④ convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_list, null,true);

⑤ convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_list, parent);

⑥ convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_list, null);

先把答案公布了,后面咱通过源码来找原因。

①的效果图:


②和⑤的效果图:


③④⑥的效果图:


好了,下面我们从源码开始找找原因。故事的起因源自LayoutInflater.from().inflate(),那么我们就从这里入手。

 /**
  * 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;
}

from()方法先通过context来获取一个系统服务LAYOUT_INFLATER_SERVICE,由于这里的LayoutInflater是个抽象方法,所以这里其实最终得到的是子类PhoneLayoutInflater的实例,PhoneLayoutInflater复写了父类的onCreateView()方法。

private static final String[] sClassPrefixList = {
        "android.widget.",
        "android.webkit."
    };

/** Override onCreateView to instantiate names that correspond to the
        widgets known to the Widget factory. If we don't find a match,
        call through to our super class.
*/
@Override 
protected View onCreateView(String name, AttributeSet attrs) throws ClassNotFoundException {
    for (String prefix : sClassPrefixList) {
        try {
            View view = createView(name, prefix, attrs);
            if (view != null) {
                return view;
            }
        } catch (ClassNotFoundException e) {
            // In this case we want to let the base class take a crack
            // at it.
        }
    }

    return super.onCreateView(name, attrs);
}

这里PhoneLayoutInflater只是把系统控件的包名前缀传给createView(),然后createView()就会解析这个控件标签返回这个控件的实例。

所以,现在我们知道通过LayoutInflater.from()方法返回的其实是PhoneLayoutInflater的实例。然后再调用inflate()方法完成真正的布局填充工作。所以,可以猜到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.
 */
public View inflate(@LayoutRes int resource, @Nullable ViewGroup root) {
    return inflate(resource, root, 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();

    final XmlResourceParser parser = res.getLayout(resource);
    try {
        return inflate(parser, root, attachToRoot);
    } finally {
        parser.close();
    }
}

这里先获取到一个Resources对象,然后通过我们传进来的资源id生成一个XmlResourceParser对象,熟悉xml解析的人对这个应该不陌生。至此我们的xml的信息都装在了XmlResourceParser对象中,然后再调用inflate(parser, root, attachToRoot)方法进行实际的解析工作。

public View inflate(XmlPullParser parser, @Nullable ViewGroup root, boolean attachToRoot) {
        synchronized (mConstructorArgs) {
        final Context inflaterContext = mContext;
        final AttributeSet attrs = Xml.asAttributeSet(parser);
        Context lastContext = (Context) mConstructorArgs[0];
        mConstructorArgs[0] = inflaterContext;
        View result = root;

        try {
            // Look for the root node.
            //寻找根布局
            int type;

            final String name = parser.getName();//拿到标签名 比如TextView

            if (TAG_MERGE.equals(name)) {//解析merge标签
                rInflate(parser, root, inflaterContext, attrs, false);
            } else {
                // temp是在xml中解析出来的根布局. 这里是指根据标签名解析了该标签,并没有解析子标签
               final View temp = createViewFromTag(root, name, inflaterContext, attrs);

                ViewGroup.LayoutParams params = null;

                if (root != null) {
                    //可以看到,只有root即我们指定的parent不为null的时候,才会使用到根布局中指定的布局参数。**这里就解释了为什么③④⑥中我们的高宽值没有生效**
                    params = root.generateLayoutParams(attrs);
                    if (!attachToRoot) {
                    //attachToRoot为false时,才会给我们xml中的跟布局设置布局参数,**这里解释了为什么①的效果跟我们预期相同**
                        // Set the layout params for temp if we are not
                        // attaching. (If we are, we use addView, below)
                        temp.setLayoutParams(params);
                    }
                }

                // Inflate all children under temp against its context.
                //这里递归调用,解析temp的所有子控件并把解析的子控件添加到temp中
                rInflateChildren(parser, temp, attrs, true);

                // We are supposed to attach all the views we found (int temp)
                // to root. Do that now.
                if (root != null && attachToRoot) {
                //这里可以看到,如果root即parent不为null且attachToRoot为true的话,就把我们xml解析出来的布局添加到root中
                    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) {
                //这里可以看到,如果root即parent为null或者attachToRoot为false,就直接把xml的跟布局返回。也就是把xml中的根布局作为根布局
                    result = temp;
                }
            }

        } catch (XmlPullParserException e) {
           
        } finally {
          
        }
        
        return result;
    }
}

至此,我们从源码中找到了①③④⑥不同效果的原因。至于为什么②和⑤会导致应用崩掉,这是因为ListView不支持addView的操作。如果root即parent不为null且attachToRoot为true(这个条件正是②和⑤给的参数)时,会走这行代码root.addView(temp, params),但ListView不支持addView,抛了异常。

/**
 * This method is not supported and throws an UnsupportedOperationException when called.
 *
 * @param child Ignored.
 * @param index Ignored.
 *
 * @throws UnsupportedOperationException Every time this method is invoked.
 */
@Override
public void addView(View child, int index) {
    throw new UnsupportedOperationException("addView(View, int) is not supported in AdapterView");
}

可以看到,如果调用listView的addView方法,直接就抛异常了。当然,这只是listView的特殊情况而已。

我们现在搞明白了开头提到的问题,也清楚了当使用LayoutInflater.from().inflate()方法时应该传递怎样的参数才能达到我们预期的效果。至于inflate解析xml的过程,我们下一篇再说。

参考

鸿洋博客

Android源码设计模式解析与实践

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 151,829评论 1 331
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 64,603评论 1 273
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 101,846评论 0 226
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 42,600评论 0 191
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 50,780评论 3 272
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 39,695评论 1 192
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,136评论 2 293
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 29,862评论 0 182
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 33,453评论 0 229
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 29,942评论 2 233
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,347评论 1 242
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 27,790评论 2 236
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,293评论 3 221
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 25,839评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,448评论 0 181
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 34,564评论 2 249
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 34,623评论 2 249

推荐阅读更多精彩内容