LayoutInflater部分源码阅读和一个意外的收获

LayoutInflater

官方介绍这个类时说:将布局XML文件实例化为其对应的View对象。而我在使用LayoutInflater最多的地方就是列表加载条目布局的时候,但其实它是一个默默无闻的类,每当Activity调用setContentView()设置布局界面时,其实都是通过LayoutInflater创建的布局。

我之前一直在想当我调用setContentView()时,到底发生了什么,直到前几天我去一点一点的看了一部分源码,我一开始做开发的时候很抵触看源码的,觉得那些是什么啊,我根本看不懂的,对,我依然还是很多很不懂,但我一直只是知道我该怎么做,不知道为什么这么做,我看了几次源码突然发现这是一个很好玩的过程,就停不下来了,不过有时候我真的是有些头疼,就是找不到一些关键的点,不理解为什么这样写,我有时候尝试去理解,但又失败了。

对,我们不能总是只做自己擅长的,不走出舒适区,怎么才能变得更好呢?所以年初我就给自己定了一个多看源码的目标,之前看完了AsyncTask,就一直没啥头绪,不知道接下来去看什么,我去看了知乎上的一篇文章,阅读ANDROID源码的一些姿势,我去看了Content,Activity的部分源码,但由于还没看完就先不讲他们,文章地址如下:

https://zhuanlan.zhihu.com/p/20564614

以下源码版本均为API25。

从setContentView()开始看源码

分成两条路才看比较好。

一个是我们默认会继承的AppCompatActivity,当我们调用setContentView(resId)时,会调用到这个方法,然后通过getDelegate()返回的AppCompatDelegate,再去调用setContentView(layoutResID),

//  设置布局  AppCompatActivity的方法
@Override
public void setContentView(@LayoutRes int layoutResID) {
        getDelegate().setContentView(layoutResID);
}

//  先去调用AppCompatDelegate.create(this, this)获取AppCompatDelegate对象。
//  它本身是一个抽象类,我们需要获取它的实例。
//  官方介绍AppCompatDelegate说,它表示一个代理,我们可以使用该代理将AppCompat的支持扩展到任何活动。
@NonNull
public AppCompatDelegate getDelegate() {
        if (mDelegate == null) {
            mDelegate = AppCompatDelegate.create(this, this);
        }
        return mDelegate;
}

//  最后落到了这里AppCompatDelegateImplV9继承自AppCompatDelegateImplBase
//  11继承了9,14继承了11,以此类推,
//  但只有AppCompatDelegateImplV9才有setContentView()
//  这意味着9,11,14,23,N都是共享这一个方法的
private static AppCompatDelegate create(Context context, Window window,
            AppCompatCallback callback) {
        final int sdk = Build.VERSION.SDK_INT;
        if (BuildCompat.isAtLeastN()) {
            return new AppCompatDelegateImplN(context, window, callback);
        } else if (sdk >= 23) {
            return new AppCompatDelegateImplV23(context, window, callback);
        } else if (sdk >= 14) {
            return new AppCompatDelegateImplV14(context, window, callback);
        } else if (sdk >= 11) {
            return new AppCompatDelegateImplV11(context, window, callback);
        } else {
            return new AppCompatDelegateImplV9(context, window, callback);
        }
}

//  AppCompatDelegateImplV9中的方法,它还有很多重载方法
@Override
public void setContentView(int resId) {
        ensureSubDecor();
        ViewGroup contentParent = (ViewGroup) mSubDecor.findViewById(android.R.id.content);
        contentParent.removeAllViews();
        //  我们找到了LayoutInflater,通过传递父布局和布局Id开始实例化View
        LayoutInflater.from(mContext).inflate(resId, contentParent);
        mOriginalWindowCallback.onContentChanged();
}

一个最普通的基类,Activity,当我们调用setContentView()时,我么走到了这里,然后通过调用Window的setContentView(layoutResID),Window类本身是一个抽象的类

public void setContentView(@LayoutRes int layoutResID) {
        getWindow().setContentView(layoutResID);
        initWindowDecorActionBar();
}

//  getWindow()获取到的实例是PhoneWindow,
mWindow = new PhoneWindow(this, window);

//  PhoneWindow中的setContentView()方法
@Override
public void setContentView(int layoutResID) {
        // Note: FEATURE_CONTENT_TRANSITIONS may be set in the process of installing the window
        // decor, when theme attributes and the like are crystalized. Do not check the feature
        // before this happens.
        //  如果没有mContentParent就先去实例出mContentParent和DecorView
        if (mContentParent == null) {
            installDecor();
        } else if (!hasFeature(FEATURE_CONTENT_TRANSITIONS)) {
            mContentParent.removeAllViews();
        }

        if (hasFeature(FEATURE_CONTENT_TRANSITIONS)) {
            final Scene newScene = Scene.getSceneForLayout(mContentParent, layoutResID,
                    getContext());
            transitionTo(newScene);
        } else {
            //  我们找到了LayoutInflater,通过传递父布局和布局Id开始实例化View
            mLayoutInflater.inflate(layoutResID, mContentParent);
        }
        mContentParent.requestApplyInsets();
        final Callback cb = getCallback();
        if (cb != null && !isDestroyed()) {
            cb.onContentChanged();
        }
        mContentParentExplicitlySet = true;
}

我们看完了两个Activity的setCotentView()后,知道了其实xmlId最后都交给了LayoutInflater的inflate方法去解析,那么我们就来进入到LayoutInflater去看看它都做了什么。

LayoutInflater内部世界(只窥探了部分)

我们先来看看LayoutInflater实例是如何获取的,它本是一个抽象类,所以需要找到它的实现类,我们都会通过LayoutInflater.from(上下文)去获取一个LayoutInflater实例,代码内部通过context的getSystemService(int)去获取实例,Context只是一个抽象类,而我们需要去它的实现类去找getSystemService(int)的实现,很幸运在我翻源码过程中ContextImpl这个类,通过追踪我发现了SystemServiceRegistry这个系统服务注册类,它通过HashMap管理所有的系统服务,而所有服务注册的过程写在了静态代码块中,可以看得出PhoneLayoutInflater就是LayoutInflater实现类,它的内部很简单,主要是实现了onCreateView()方法。

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

//  ContextImpl里实现了这个方法
@Override
public Object getSystemService(String name) {
        return SystemServiceRegistry.getSystemService(this, name);
}

//  SystemServiceRegistry静态代码块中的各种服务,这是LayoutInflater的服务注册
registerService(Context.LAYOUT_INFLATER_SERVICE, LayoutInflater.class,
                new CachedServiceFetcher<LayoutInflater>() {
            @Override
            public LayoutInflater createService(ContextImpl ctx) {
                return new PhoneLayoutInflater(ctx.getOuterContext());
}});

接下来就让走入inflate()函数,我们调用的是双参数的方法,最后都会落到inflate(XmlPullParser parser, @Nullable ViewGroup root, boolean attachToRoot)上面去,首先我们需要拿到一个XmlResourceParser,它是通过XmlBlock最后返回给我们的一个 XmlResourceParser的实现类,然后可以通过下面给源码中添加的注释来查看大致的意思:

//  双参数的inflate
return inflate(parser, root, root != null);

//  落到这个函数上
public View inflate(@LayoutRes int resource, @Nullable ViewGroup root, boolean attachToRoot) {
        //  先拿到Resources   
        final Resources res = getContext().getResources();
        //  省略部分无关代码
        // 通过 Resources的getLayout拿到一个Xml解析器
        //  最后会跟踪到XmlBlock中
        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) {
        synchronized (mConstructorArgs) {
            //  省略无关代码
            final Context inflaterContext = mContext;
            final AttributeSet attrs = Xml.asAttributeSet(parser);
            Context lastContext = (Context) mConstructorArgs[0];
            //  上下文对象
            mConstructorArgs[0] = inflaterContext;
            //  保存父视图
            View result = root;
            try {
                // 找到root节点.
                int type;
                while ((type = parser.next()) != XmlPullParser.START_TAG &&
                        type != XmlPullParser.END_DOCUMENT) {
                    // Empty
                }
                //  如果不是一个标签的就报错
                if (type != XmlPullParser.START_TAG) {
                    throw new InflateException(parser.getPositionDescription()
                            + ": No start tag found!");
                }
                //  调用了本地方法获取到了name
                final String name = parser.getName();
                //  如果是merge标签,必须要有父视图
                if (TAG_MERGE.equals(name)) {
                    if (root == null || !attachToRoot) {
                        throw new InflateException("<merge /> can be used only with a valid "
                                + "ViewGroup root and attachToRoot=true");
                    }
                    //  然后调用rInflate()
                    rInflate(parser, root, inflaterContext, attrs, false);
                } else {
                    // 不是merge标签就直接解析 就直接解析布局中的视图
                    //  创建View通过tag,name是控件名,如TextView
                    final View temp = createViewFromTag(root, name, inflaterContext, attrs);
                    ViewGroup.LayoutParams params = null;
                    if (root != null) {                
                        // Create layout params that match root, if supplied
                        //  创建布局参数
                        params = root.generateLayoutParams(attrs);
                        if (!attachToRoot) {
                            // Set the layout params for temp if we are not
                            // attaching. (If we are, we use addView, below)
                            //  如果没绑定父视图就给temp设置布局参数
                            temp.setLayoutParams(params);
                        }
                    }
                    // Inflate all children under temp against its context.
                    //  解析temp下的所有子VIew
                    rInflateChildren(parser, temp, attrs, true);
                    // We are supposed to attach all the views we found (int temp)
                    // to root. Do that now.
                    //  有父视图时就将temp添加进去
                    if (root != null && attachToRoot) {
                        root.addView(temp, params);
                    }
                    // Decide whether to return the root that was passed in or the
                    // top view found in xml.
                    //  没有父视图就将temp作为父视图赋值给result 
                    if (root == null || !attachToRoot) {
                        result = temp;
                    }
                }
            }
            //  省略代码
            return result;
        }
}

//  final View temp = createViewFromTag(root, name, inflaterContext, attrs);
//  下面我们去看看createViewFromTag方法中的去看看
//  最后落到了View createViewFromTag(View parent, String name, Context context, AttributeSet attrs, boolean ignoreThemeAttr)
View createViewFromTag(View parent, String name, Context context, AttributeSet attrs,
            boolean ignoreThemeAttr) {
        if (name.equals("view")) {
            name = attrs.getAttributeValue(null, "class");
        }
        // Apply a theme wrapper, if allowed and one is specified.
        //  如果不忽视主题,可以为控件设置自己的样式
        if (!ignoreThemeAttr) {
            final TypedArray ta = context.obtainStyledAttributes(attrs, ATTRS_THEME);
            final int themeResId = ta.getResourceId(0, 0);
            if (themeResId != 0) {
                context = new ContextThemeWrapper(context, themeResId);
            }
            ta.recycle();
        }
        //  对这就是的意外收获,会在结尾处展示出来它的使用效果
        if (name.equals(TAG_1995)) {
            // Let's party like it's 1995!
            return new BlinkLayout(context, attrs);
        }
        //  使用Factory来解析View
        try {
            View view;
            if (mFactory2 != null) {
                view = mFactory2.onCreateView(parent, name, context, attrs);
            } else if (mFactory != null) {
                view = mFactory.onCreateView(name, context, attrs);
            } else {
                view = null;
            }
            //  如果View还是为空就用私有的Factory去解析
            if (view == null && mPrivateFactory != null) {
                view = mPrivateFactory.onCreateView(parent, name, context, attrs);
            }
            //  如果View还是为空就通过onCreateView或者createView去创建VIew
            if (view == null) {
                final Object lastContext = mConstructorArgs[0];
                mConstructorArgs[0] = context;
                try {
                    if (-1 == name.indexOf('.')) {
                        //  如果不包含.就说明这是系统自带的控件
                        view = onCreateView(parent, name, attrs);
                    } else {
                        //  如果包含.说明这是一个自定义布局,因为控件标签要写明全路径
                        //  (也有有些例外的系统控件带.)
                        view = createView(name, null, attrs);
                    }
                } finally {
                    mConstructorArgs[0] = lastContext;
                }
            }

            return view;
        }
        //  省略代码
}

//  先去看看系统的View创建
//  因为PhoneLayoutInflater重写了onCreateView
//  PhoneLayoutInflater提供的可拼接前缀
private static final String[] sClassPrefixList = {
        "android.widget.",
        "android.webkit.",
        "android.app."
};
@Override protected View onCreateView(String name, AttributeSet attrs) throws ClassNotFoundException {
        for (String prefix : sClassPrefixList) {
            try {
                //  如果拼接这些前缀可以拿到控件,那么最后还是去调用父类的createView()
                View view = createView(name, prefix, attrs);
                if (view != null) {
                    return view;
                }
            } 
            //  省略代码
        }
        //  如果前缀没有找到调用父类的onCreateView并使用android.view.去创建View。
        //  但最后还是会走到createView(),只是传递过去的前缀是android.view.
        return super.onCreateView(name, attrs);
}

//  下面我们来看看这个创建VIew的createView()方法,在LayoutInflater中
public final View createView(String name, String prefix, AttributeSet attrs)
            throws ClassNotFoundException, InflateException {
        //  从事先准备好的HashMap缓存中获取构造函数
        Constructor<? extends View> constructor = sConstructorMap.get(name);
        if (constructor != null && !verifyClassLoader(constructor)) {
            constructor = null;
            sConstructorMap.remove(name);
        }
        Class<? extends View> clazz = null;
        try {
            //  缓存中没找到构造函数
            if (constructor == null) {
                // Class not found in the cache, see if it's real, and try to add it
                //  如果前缀不为空就构建完整的View路径,并加载该类
                clazz = mContext.getClassLoader().loadClass(
                        prefix != null ? (prefix + name) : name).asSubclass(View.class);
                //  省略代码
                //  从 clazz中再次获取构造函数
                constructor = clazz.getConstructor(mConstructorSignature);
                constructor.setAccessible(true);
                //  并添加到缓存中,66666
                sConstructorMap.put(name, constructor);
            } else {
                 //  已经存缓存中拿到构造函数了就省略下面的代码
            }
            Object[] args = mConstructorArgs;
            args[1] = attrs;
            //  通过反射获取到了View
            final View view = constructor.newInstance(args);
            //  对ViewStub进行了处理
            if (view instanceof ViewStub) {
                // Use the same context when inflating ViewStub later.
                final ViewStub viewStub = (ViewStub) view;
                viewStub.setLayoutInflater(cloneInContext((Context) args[0]));
            }
            return view;
        } 
        //  省略代码
}

//  还有最后函数需要看rInflate()
//  inflate()中如果是merge会直接调用rInflate(),如果不是merge,会调用rInflateChildren()
//  而rInflateChildren()最后也会落到rInflate()上去解析容器中的控件
void rInflate(XmlPullParser parser, View parent, Context context,
            AttributeSet attrs, boolean finishInflate) throws XmlPullParserException, IOException {
        //  先获取视图树的深度
        final int depth = parser.getDepth();
        int type;
        //  一个个元素解析
        while (((type = parser.next()) != XmlPullParser.END_TAG ||
                parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
            if (type != XmlPullParser.START_TAG) {
                continue;
            }
            //  获取控件名称
            final String name = parser.getName();           
            //  根据不同的标签去判断
            if (TAG_REQUEST_FOCUS.equals(name)) {
                parseRequestFocus(parser, parent);
            } else if (TAG_TAG.equals(name)) {
                parseViewTag(parser, parent, attrs);
            } else if (TAG_INCLUDE.equals(name)) {
                if (parser.getDepth() == 0) {
                    throw new InflateException("<include /> cannot be the root element");
                }
                parseInclude(parser, context, parent, attrs);
            } else if (TAG_MERGE.equals(name)) {
                throw new InflateException("<merge /> must be the root element");
            } else {
                //  如果都是
                final View view = createViewFromTag(parent, name, context, attrs);
                final ViewGroup viewGroup = (ViewGroup) parent;
                final ViewGroup.LayoutParams params = viewGroup.generateLayoutParams(attrs);
                //  这里会递归解析控件,然后添加到容器中
                rInflateChildren(parser, view, attrs, true);
                viewGroup.addView(view, params);
            }
        }
        if (finishInflate) {
            parent.onFinishInflate();
        }
    }

这就是我LayoutInflater部分源码阅读,当然这之中借助了Android源码设计模式的帮助,不过书中的源码版本比较低,所有有些需要我自己来看,过程却是很长,但是通过注释也算是可以进一步理解LayoutInflater,俺么接下来我们来看看我的意外收获。

TAG_1995

我在看源码的过程中看到了不少标签,比如最常见的TAG_MERGE和TAG_INCLUDE,但是看到TAG_1995我很好奇,这个我怎么从来都没见过这个blink标签,它的效果是什么呢?Google后我才知道,其实这个一个可以一闪一闪的标签,下面是布局代码和效果图:

//  LayoutInflater中的部分常量
private static final String TAG_MERGE = "merge";
private static final String TAG_INCLUDE = "include";
private static final String TAG_1995 = "blink";
...
//  createViewFromTag()中写到
if (name.equals(TAG_1995)) {
        // Let's party like it's 1995!
        return new BlinkLayout(context, attrs);
}

//  我的布局
<?xml version="1.0" encoding="utf-8"?>
<blink
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/content_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.google.jaaaelu.gzw.readsource.MainActivity"
    tools:showIn="@layout/activity_main">

    <TextClock
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="Hello World!"/>

    <TextClock
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="right"
        android:text="Hello World!"/>

    <TextClock
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="left|bottom"
        android:text="Hello World!"/>

    <TextClock
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="right|bottom"
        android:text="Hello World!"/>

    <TextClock
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:text="Hello World!"/>

</blink>
bulingbuling.gif

效果是不是很酷,一闪一闪的,虽然我们也可以手动实现这个效果,但是既然原生Android已经提供了,我们也就不必自己去实现了。

我可能看源码的过程中会有一些错误,如果你看到了什么错误,可以及时和我沟通,不懂的地方也可以及时问我,我不知道我会去及时Google的,我很喜欢和菜头的那句话,请你相信我,我所说的每句话,都是错误的。

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

推荐阅读更多精彩内容