当前位置: 移动技术网 > IT编程>移动开发>Android > Android中LayoutInflater.inflater()的正确打开方式

Android中LayoutInflater.inflater()的正确打开方式

2019年07月31日  | 移动技术网IT编程  | 我要评论

h动漫下载网,仙意通玄,谁与玩芳草

前言

layoutinflater在开发中使用频率很高,但是一直没有太知道layoutinflater.from(context).inflate()的真正用法,今天就看看源码的流程。

首先来看from()的源码:

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

其实就是从context中获取context.layout_inflater_service所对应的系统服务。这里涉及到context实现以及服务创建的源码,不继续深究。

重点是通常所使用的inflate()方法,比较常用的就是这两个:

  • inflate(@layoutres int resource, @nullable viewgroup root)
  • inflate(@layoutres int resource, @nullable viewgroup root, boolean attachtoroot)

另外两个方法inflate(xmlpullparser parser, @nullable viewgroup root)inflate(xmlpullparser parser, @nullable viewgroup root, boolean attachtoroot)

而两个参数的方法,实际也是调用了三个参数的inflate()方法,只是在三个参数传入了root!=null

public view inflate(@layoutres int resource, @nullable viewgroup root) {
 return inflate(resource, root, root != null);
}

那我们就可以直接看三个参数的inflate()方法了,其中res.getlayout(resource)这句代码,已经将我们传入的layout布局的根布局的xml属性都加载到了xmlresourceparser中

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

这里其实就会发现,最后return调用的其实是inflate(xmlpullparser parser, @nullable viewgroup root, boolean attachtoroot)这个方法,所谓的四个inflate()方法,其他三个只是对这个方法的重载,主要代码还是在这个方法中实现的

这部分代码较长,以注释的形式解释代码

public view inflate(xmlpullparser parser, @nullable viewgroup root, boolean attachtoroot) {
 synchronized (mconstructorargs) {
 trace.tracebegin(trace.trace_tag_view, "inflate");

 final context inflatercontext = mcontext;
 //1.通过xmlresourceparser对象转换成attributeset
 final attributeset attrs = xml.asattributeset(parser);
 context lastcontext = (context) mconstructorargs[0];
 mconstructorargs[0] = inflatercontext;
 view result = root;

 try {
  //2.在xml中寻找根节点,如果类型是xmlpullparser.start_tag或者xmlpullparser.end_document就会退出循环
  int type;
  while ((type = parser.next()) != xmlpullparser.start_tag &&
   type != xmlpullparser.end_document) {
  // empty
  }
  //3.如果根节点类型不是xmlpullparser.start_tag将抛出异常
  if (type != xmlpullparser.start_tag) {
  throw new inflateexception(parser.getpositiondescription()
   + ": no start tag found!");
  }

  final string name = parser.getname();

  //4.判断根节点是否是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(parser, root, inflatercontext, attrs, false);
  } else {
  //5.通过根节点创建临时的view对象
  final view temp = createviewfromtag(root, name, inflatercontext, attrs);

  viewgroup.layoutparams params = null;

  if (root != null) {
   //6.如果root不为空,则调用generatelayoutparams(attrs)获取root所对应layoutparams对象
   params = root.generatelayoutparams(attrs);
   //是否attachtoroot
   if (!attachtoroot) {
   //7.如果attachtoroot为false,则使用root默认的layoutparams作为临时view对象的属性
   temp.setlayoutparams(params);
   }
  }

  //8.inflate xml的所有子节点
  rinflatechildren(parser, temp, attrs, true);

  //9.判断是否需要将创建的临时view attach到root中
  if (root != null && attachtoroot) {
   root.addview(temp, params);
  }

  //10.决定方法的返回值是root还是临时view
  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 {
  mconstructorargs[0] = lastcontext;
  mconstructorargs[1] = null;

  trace.traceend(trace.trace_tag_view);
 }

 return result;
 }
}

1中的xmlresourceparser在之前所获取的,包含了layout中跟布局的属性数据。

6,7则是很多时候使用inflate方法之后,发现xml布局设置的宽高属性不生效的部分原因,有时候在recyclerview中添加就会这样。如果root!=null且attachtoroot为false时,创建的view则会具有自身根节点属性值,与root对应的layoutparam

9的判断决定了创建的view是否添加到root中,而10则决定了方法返回的是root还是view

总结

根据inflate的参数不同可以获得不同的返回值

root attachtoroot 返回值
null false(或者true) 返回resource对应的view对象,但是xml中根节点的属性没有生效
!=null false 返回resource对应的view对象,并且xml中根节点的属性生效,view对象的layoutparam与root的layoutparam对应
!=null true 返回root对象,对应resource创建的view对象,xml中根节点的属性生效,并且将会添加到root中

注意:attachtoroot默认为root!=null的值

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对移动技术网的支持。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网