简析LayoutInflate工作流程

发表于:2018-5-22 16:14

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:牛板腩天下第一    来源:51testing软件测试网采编

  看完郭神的解析后,我打算做一些简单的总结。一开始了解到的东西是LayoutInflate,既然是源码分析,那就得先能查看源码,查看源码的方法是打开你的Android Studio点击File -》Setting -》搜索框中搜索Android SDK -》Show Package Details -》下载source for Android 23(因为我手机是安卓6.0版本我就习惯性的下了6.0的源码,最好还是下最新版本的源码)
  先来研究的东西是LayoutInflate:
  不管你用哪个inflate方法的重载你都会转到inflate(XmlPullParser?parser,?ViewGroup?root,?boolean?attachToRoot)中
  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(" 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;
  }
  }
  可以看到他先用pull解析方式来解析xml文件,再通过createViewFromTag()把参数传给他,你要是在点进去查看该方法你就可以知道它是用createView()来创建并返回一个View的实例的,再通过rInflate()查找view下的子元素并逐一添加进父布局中,随着版本更新迭代,源码中多了一个rInflateChildren(),实际上他也调用了rInflate(),这样做的目的是填充所有子布局。
  我们在调用setContentView()时,Android会自动在布局文件的最外层再嵌套一个帧布局,如果没有外面套着的这一层布局就无法设置该布局的width和height



上文内容不用于商业目的,如涉及知识产权问题,请权利人联系博为峰小编(021-64471599-8017),我们将立即处理。
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号