Android LayoutInflater 类分析
我们事先准备好一个XML布局,如下
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical">
- <TextView />
- <LinearLayout />
- <Button />
- </LinearLayout />
- </RelativeLayout>
- LayoutInflater.from(this).inflate(R.layout.activity_main,null);
只有这么简单的一行代码,首先调用from函数,获取LayoutInflater对象,然后在调用该对象下的inflate方法,我们先看一下from方法,看看LayoutInflater的实现类是哪个
- registerService(Context.LAYOUT_INFLATER_SERVICE, LayoutInflater.class,
- new CachedServiceFetcher<LayoutInflater>() {
- @Override
- public LayoutInflater createService(ContextImpl ctx) {
- return new PhoneLayoutInflater(ctx.getOuterContext());
- }}
- );
接下来我们继续看inflate方法实现,因为继承关系缘故,inflate是在LayoutInflater中实现,后边会用到PhoneLayoutInflater
- public View inflate(@LayoutRes int resource, @Nullable ViewGroup root) {
- return inflate(resource, root, root != null);
- }
一个很简单的调用,把传入的参数继续向内部传入
- 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();
- }
- }
这里边只是获取了一下XML的解析器,然后继续调用重载函数,我们继续往下看,简单看一下就行,我下面会说这个函数做了什么 操作
- public View inflate(XmlPullParser parser, @Nullable ViewGroup root, boolean attachToRoot) {
- synchronized (mConstructorArgs) {
- Trace.traceBegin(Trace.TRACE_TAG_VIEW, "inflate");
- 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;
- 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!");
- }
- final String name = parser.getName();
- if (DEBUG) {
- System.out.println("**************************");
- System.out.println("Creating root view: "
- + name);
- System.out.println("**************************");
- }
- 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(parser, root, inflaterContext, attrs, false);
- } else {
- // Temp is the root view that was found in the xml
- final View temp = createViewFromTag(root, name, inflaterContext, attrs);
- ViewGroup.LayoutParams params = null;
- if (root != null) {
- if (DEBUG) {
- System.out.println("Creating params from root: " +
- root);
- }
- // 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.setLayoutParams(params);
- }
- }
- if (DEBUG) {
- System.out.println("-----> start inflating children");
- }
- // Inflate all children under temp against its context.
- rInflateChildren(parser, temp, attrs, true);
- if (DEBUG) {
- System.out.println("-----> done inflating children");
- }
- // We are supposed to attach all the views we found (int temp)
- // to root. Do that now.
- 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.
- if (root == null || !attachToRoot) {
- result = temp;
- }
- }
- } catch (XmlPullParserException e) {
- final InflateException ie = new InflateException(e.getMessage(), e);
- ie.setStackTrace(EMPTY_STACK_TRACE);
- throw ie;
- } catch (Exception e) {
- final InflateException ie = new InflateException(parser.getPositionDescription()
- + ": " + e.getMessage(), e);
- ie.setStackTrace(EMPTY_STACK_TRACE);
- throw ie;
- } finally {
- // Don't retain static reference on context.
- mConstructorArgs[0] = lastContext;
- mConstructorArgs[1] = null;
- Trace.traceEnd(Trace.TRACE_TAG_VIEW);
- }
- return result;
- }
- }
- private View createViewFromTag(View parent, String name, Context context, AttributeSet attrs) {
- return createViewFromTag(parent, name, context, attrs, false);
- }
这个方法是重载的调用,我们继续往下看
- 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);
- }
- 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;
- }
- if (view == null && mPrivateFactory != null) {
- view = mPrivateFactory.onCreateView(parent, name, context, attrs);
- }
- 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;
- } catch (InflateException e) {
- throw e;
- } catch (ClassNotFoundException e) {
- final InflateException ie = new InflateException(attrs.getPositionDescription()
- + ": Error inflating class " + name, e);
- ie.setStackTrace(EMPTY_STACK_TRACE);
- throw ie;
- } catch (Exception e) {
- final InflateException ie = new InflateException(attrs.getPositionDescription()
- + ": Error inflating class " + name, e);
- ie.setStackTrace(EMPTY_STACK_TRACE);
- throw ie;
- }
- }
咱们最普通的用法都是用系统的加载方式
- 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;
- }
- }
- 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 {
- 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);
- }
这个子类的onCreateView方法主要功能是通过循环sClassPrefixList这个数组并调用createView尝试创建view,如果可以创建成功便返回,如果循环结束后仍然没有创建成功便调用LayoutInflater的ch
- protected View onCreateView(String name, AttributeSet attrs)
- throws ClassNotFoundException {
- return createView(name, "android.view.", attrs);
- }
我们来往下看createView把
- public final View createView(String name, String prefix, AttributeSet attrs)
- throws ClassNotFoundException, InflateException {
- Constructor<? extends View> constructor = sConstructorMap.get(name);
- if (constructor != null && !verifyClassLoader(constructor)) {
- constructor = null;
- sConstructorMap.remove(name);
- }
- Class<? extends View> clazz = null;
- try {
- Trace.traceBegin(Trace.TRACE_TAG_VIEW, name);
- if (constructor == null) {
- // Class not found in the cache, see if it's real, and try to add it
- clazz = mContext.getClassLoader().loadClass(
- prefix != null ? (prefix + name) : name).asSubclass(View.class);
- if (mFilter != null && clazz != null) {
- boolean allowed = mFilter.onLoadClass(clazz);
- if (!allowed) {
- failNotAllowed(name, prefix, attrs);
- }
- }
- constructor = clazz.getConstructor(mConstructorSignature);
- constructor.setAccessible(true);
- sConstructorMap.put(name, constructor);
- } else {
- // If we have a filter, apply it to cached constructor
- if (mFilter != null) {
- // Have we seen this name before?
- Boolean allowedState = mFilterMap.get(name);
- if (allowedState == null) {
- // New class -- remember whether it is allowed
- clazz = mContext.getClassLoader().loadClass(
- prefix != null ? (prefix + name) : name).asSubclass(View.class);
- boolean allowed = clazz != null && mFilter.onLoadClass(clazz);
- mFilterMap.put(name, allowed);
- if (!allowed) {
- failNotAllowed(name, prefix, attrs);
- }
- } else if (allowedState.equals(Boolean.FALSE)) {
- failNotAllowed(name, prefix, attrs);
- }
- }
- }
- Object lastContext = mConstructorArgs[0];
- if (mConstructorArgs[0] == null) {
- // Fill in the context if not already within inflation.
- mConstructorArgs[0] = mContext;
- }
- Object[] args = mConstructorArgs;
- args[1] = attrs;
- final View view = constructor.newInstance(args);
- if (view instanceof ViewStub) {
- // Use the same context when inflating ViewStub later.
- final ViewStub viewStub = (ViewStub) view;
- viewStub.setLayoutInflater(cloneInContext((Context) args[0]));
- }
- mConstructorArgs[0] = lastContext;
- return view;
- } catch (NoSuchMethodException e) {
- final InflateException ie = new InflateException(attrs.getPositionDescription()
- + ": Error inflating class " + (prefix != null ? (prefix + name) : name), e);
- ie.setStackTrace(EMPTY_STACK_TRACE);
- throw ie;
- } catch (ClassCastException e) {
- // If loaded class is not a View subclass
- final InflateException ie = new InflateException(attrs.getPositionDescription()
- + ": Class is not a View " + (prefix != null ? (prefix + name) : name), e);
- ie.setStackTrace(EMPTY_STACK_TRACE);
- throw ie;
- } catch (ClassNotFoundException e) {
- // If loadClass fails, we should propagate the exception.
- throw e;
- } catch (Exception e) {
- final InflateException ie = new InflateException(
- attrs.getPositionDescription() + ": Error inflating class "
- + (clazz == null ? "<unknown>" : clazz.getName()), e);
- ie.setStackTrace(EMPTY_STACK_TRACE);
- throw ie;
- } finally {
- Trace.traceEnd(Trace.TRACE_TAG_VIEW);
- }
- }
这个这个函数便是先把前缀prefix和name进行了一个拼接,然后使用反射技术通过拼接出来的包名创建对应的view对象,最终返回创建出来的view,到这里就弄清楚了view是怎么创建出来的。这快可以告一段落了,我们往下继续看是怎么把一个庞大的XML布局转换成一个ViewGroup对象的
我们看一下rInflateChildren函数的具体实现把
- final void rInflateChildren(XmlPullParser parser, View parent, AttributeSet attrs,
- boolean finishInflate) throws XmlPullParserException, IOException {
- rInflate(parser, parent, parent.getContext(), attrs, finishInflate);
- }
- void rInflate(XmlPullParser parser, View parent, Context context,
- AttributeSet attrs, boolean finishInflate) throws XmlPullParserException, IOException {
- final int depth = parser.getDepth();
- int type;
- boolean pendingRequestFocus = false;
- 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)) {
- pendingRequestFocus = true;
- consumeChildElements(parser);
- } 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 (pendingRequestFocus) {
- parent.restoreDefaultFocus();
- }
- if (finishInflate) {
- parent.onFinishInflate();
- }
- }
- final View temp = createViewFromTag(root, name, inflaterContext, attrs);
- ViewGroup.LayoutParams params = null;
- if (root != null) {
- if (DEBUG) {
- System.out.println("Creating params from root: " +
- root);
- }
- // 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.setLayoutParams(params);
- }
- }
- if (DEBUG) {
- System.out.println("-----> start inflating children");
- }
- // Inflate all children under temp against its context.
- rInflateChildren(parser, temp, attrs, true);
- if (DEBUG) {
- System.out.println("-----> done inflating children");
- }
- // We are supposed to attach all the views we found (int temp)
- // to root. Do that now.
- 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.
- if (root == null || !attachToRoot) {
- result = temp;
- }
Android LayoutInflater 类分析的更多相关文章
- [Android FrameWork 6.0源码学习] LayoutInflater 类分析
LayoutInflater是用来解析XML布局文件,然后生成对象的ViewTree的工具类.是这个工具类的存在,才能让我们写起Layout来那么省劲. 我们接下来进去刨析,看看里边的奥秘 //调用i ...
- Android LayoutInflater原理分析,带你一步步深入了解View(一)
转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/12921889 有不少朋友跟我反应,都希望我可以写一篇关于View的文章,讲一讲Vi ...
- Android LayoutInflater原理分析
相信接触Android久一点的朋友对于LayoutInflater一定不会陌生,都会知道它主要是用于加载布局的.而刚接触Android的朋友可能对LayoutInflater不怎么熟悉,因为加载布局的 ...
- 安卓主activity引用自定义的View——Android LayoutInflater原理分析
相信接触Android久一点的朋友对于LayoutInflater一定不会陌生,都会知道它主要是用于加载布局的.而刚接触Android的朋友可能对LayoutInflater不怎么熟悉,因为加载布局的 ...
- Android LruCache类分析
public class LurCache<K, V> { private final LinkedHashMap<K, V> map; private int size; / ...
- [旧][Android] LayoutInflater 工作流程
备注 原发表于2016.06.20,资料已过时,仅作备份,谨慎参考 前言 感觉很长时间没写文章了,这个星期因为回家和处理项目问题,还是花了很多时间的.虽然知道很多东西如果只是看一下用一次,很快就会遗忘 ...
- Android的消息循环机制 Looper Handler类分析
Android的消息循环机制 Looper Handler类分析 Looper类说明 Looper 类用来为一个线程跑一个消息循环. 线程在默认情况下是没有消息循环与之关联的,Thread类在ru ...
- 「Android」消息驱动Looper和Handler类分析
Android系统中的消息驱动工作原理: 1.有一个消息队列,可以往这个消息队列中投递消息; 2.有一个消息循环,不断的从消息队列中取得消息,然后处理. 工作流程: 1.事件源将待处理的消息加入到消息 ...
- 源码分析篇 - Android绘制流程(三)requestLayout()与invalidate()流程及Choroegrapher类分析
本文主要探讨能够触发performTraversals()执行的invalidate().postInvalidate()和requestLayout()方法的流程.在调用这三个方法到最后执行到per ...
随机推荐
- 剑指Offer全解
二维数组中的查找 描述 在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序.请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中 ...
- JDK设计模式之—动态代理
代理模式的特点 代理模式是常用的java设计模式,它的特征是代理类与委托类有同样的接口.代理类主要负责为委托类预处理消息.过滤消息.把消息转发给委托类. 代理类的对象并不是真正实现服务,而是通过调用委 ...
- [Swift]LeetCode405. 数字转换为十六进制数 | Convert a Number to Hexadecimal
Given an integer, write an algorithm to convert it to hexadecimal. For negative integer, two’s compl ...
- [Swift]LeetCode488. 祖玛游戏 | Zuma Game
Think about Zuma Game. You have a row of balls on the table, colored red(R), yellow(Y), blue(B), gre ...
- [Swift]LeetCode540. 有序数组中的单一元素 | Single Element in a Sorted Array
Given a sorted array consisting of only integers where every element appears twice except for one el ...
- [Swift]LeetCode670. 最大交换 | Maximum Swap
Given a non-negative integer, you could swap two digits at most once to get the maximum valued numbe ...
- dpkg: 处理软件包 xxx (--configure)时出错 解决办法
第一步:备份 $ sudo mv /var/lib/dpkg/info /var/lib/dpkg/info.bk 第二步:新建 $ sudo mkdir /var/lib/dpkg/info 第三步 ...
- Spring Cloud Greenwich 正式发布,Hystrix 即将寿终正寝。。
Spring Cloud Greenwich 正式版在 01/23/2019 这天正式发布了,下面我们来看下有哪些更新内容. 生命周期终止提醒 Spring Cloud Edgware Edgware ...
- WebView 讲义
http://reezy.me/p/20170515/android-webview/ (比较全面) 参考 https://developer.android.com/reference/andro ...
- ES 02 - 部署Elasticsearch单机服务 + 部署中的常见问题
目录 1 准备工作 1.1 安装JDK 1.2 下载安装包 1.3 创建elastic用户 2 启动ES服务 2.1 修改配置文件 2.2 启动服务 3 验证ES服务是否可用 4 关闭与重启服务 4. ...