当前位置: 首页 > wzjs >正文

顺义网站建设网站建设与小程序开发熊掌号

顺义网站建设,网站建设与小程序开发熊掌号,网站后台管理模板psd,网站空间换了 使用原有域名前言 对于LayoutInflater.from(this).inflate()方法的几个参数以及用法总是迷迷糊糊,源码看了忘,忘了看,因此决定写这篇博客做下记录。 源码解析 我们知道,调用LayoutInflater.from(this).inflate()方法最终都会走三参的方法 p…

前言

对于LayoutInflater.from(this).inflate()方法的几个参数以及用法总是迷迷糊糊,源码看了忘,忘了看,因此决定写这篇博客做下记录。

源码解析

我们知道,调用LayoutInflater.from(this).inflate()方法最终都会走三参的方法
public View inflate(@LayoutRes int resource, @Nullable ViewGroup root, boolean attachToRoot)
,这里先简单说明下三个参数:

resource:表示需要加载资源的id;

root:表示resource资源需要被添加的根布局;

attachToRoot:表示resource是否需要被绑定到root上;

接下来我们重点分析下相关源码

inflate(@LayoutRes int resource, @Nullable ViewGroup root, boolean attachToRoot)

    public View inflate(@LayoutRes int resource, @Nullable ViewGroup root, boolean attachToRoot) {final Resources res = getContext().getResources();...View view = tryInflatePrecompiled(resource, res, root, attachToRoot);if (view != null) {return view;}XmlResourceParser parser = res.getLayout(resource);try {return inflate(parser, root, attachToRoot);//重点看这个代码} finally {parser.close();}}

inflate(parser, root, attachToRoot)

  public View inflate(XmlPullParser parser, @Nullable ViewGroup root, boolean attachToRoot) {synchronized (mConstructorArgs) {...View result = root; //result默认赋值为roottry {...if (TAG_MERGE.equals(name)) {.... //merge标签 这里不关注} else {// 将temp赋值为xml资源的根布局viewfinal View temp = createViewFromTag(root, name, inflaterContext, attrs);ViewGroup.LayoutParams params = null;if (root != null) {//如果传入的root参数不为空,则创建root对应的LayoutParamsparams = root.generateLayoutParams(attrs);if (!attachToRoot) {//如果传入的attachToRoot为false,则给temp布局设置LayoutParamstemp.setLayoutParams(params);}}// 绑定childrenrInflateChildren(parser, temp, attrs, true);...if (root != null && attachToRoot) {//如果root不为null并且attachToRoot为true时,将temp添加到root中,并设置对应的LayoutParamsroot.addView(temp, params);}if (root == null || !attachToRoot) {// root为null 或者 attachToRoot为false时,result = tempresult = 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(getParserStateDescription(inflaterContext, attrs)+ ": " + 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; //返回结果}}

总结

  • 调用LayoutInflater.from(this).inflate(resource,null)不会将resource添加到布局中,结果返回resource资源根布局view,但不会设置对应的父布局LayoutParams属性【resource布局文件最外层的layout_width和layout_height等跟父布局有关的位置属性都会失效】;

  • 调用LayoutInflater.from(this).inflate(resource,root)会将resource添加到root布局中,同时会设置对应的父布局LayoutParams属性,结果返回root本身;

  • 调用LayoutInflater.from(this).inflate(resource,root,true)LayoutInflater.from(this).inflate(resource,root)方法效果一致,结果返回root本身;

  • 调用LayoutInflater.from(this).inflate(resource,root,false)不会将resource添加到root中,结果返回resource资源根布局view,同时会设置对应的父布局LayoutParams属性

  • 下面代码效果相同;

LayoutInflater.from(this).inflate(resource,root)
等同于
LayoutInflater.from(this).inflate(resource,root,true)
等同于
View resourceView = LayoutInflater.from(this).inflate(resource,root,false)
root.addView(resourceView)

结语

如果以上文章对您有一点点帮助,希望您不要吝啬的点个赞加个关注,您每一次小小的举动都是我坚持写作的不懈动力!ღ( ´・ᴗ・` )


文章转载自:

http://XqS6qwB2.hyhqd.cn
http://NVNzrjVQ.hyhqd.cn
http://o5s6lDJ9.hyhqd.cn
http://V9YybCHy.hyhqd.cn
http://aRmtj9aK.hyhqd.cn
http://e8RaycJl.hyhqd.cn
http://zq0mrXsV.hyhqd.cn
http://ojjiICA7.hyhqd.cn
http://v2kWOtzH.hyhqd.cn
http://IV5Xg4Ix.hyhqd.cn
http://9hcBGYB5.hyhqd.cn
http://TyhD50j1.hyhqd.cn
http://7HlSdqAt.hyhqd.cn
http://uSVnf4yJ.hyhqd.cn
http://wGQDyzsn.hyhqd.cn
http://Lal6xCrC.hyhqd.cn
http://TBReojb5.hyhqd.cn
http://UbISDuxu.hyhqd.cn
http://wElMMwLm.hyhqd.cn
http://fzi7itlj.hyhqd.cn
http://dr4HRtRh.hyhqd.cn
http://WS1pQRiD.hyhqd.cn
http://QlmfFuJw.hyhqd.cn
http://9pEhNtdL.hyhqd.cn
http://uaHxFCUm.hyhqd.cn
http://1cpBqOpg.hyhqd.cn
http://KKS8GXa7.hyhqd.cn
http://MYYS5y0f.hyhqd.cn
http://EKFaNpJd.hyhqd.cn
http://1sCrerAV.hyhqd.cn
http://www.dtcms.com/wzjs/751718.html

相关文章:

  • 大连网站建设公司哪家好一个服务器可以建几个网站
  • 济南卓远网站建设公司管理者应具备的能力
  • 网站建设h5是指的那一块福州市鼓楼区建设局网站
  • 六安网站建设公司作文网投稿
  • asp网站域名设计公司网站什么重要
  • 网站建设时间如何查询网站列表设计
  • 公司官方网站建设需要多少钱陕西咸阳建设银行网站
  • 设计师 英文网站鲅鱼圈网站在哪做
  • 新乡谷雨网络公司做的网站怎么样他达拉非片
  • 营口公司网站建设jsp做的网站答辩问题
  • 如何建设自己企业网站做包子网站
  • 网站建设对接流程扁平风格企业网站源码
  • 贵州高端网站开发网络营销顾问工作内容
  • 网站信息安全保障制度建设情况营销型网站建设专家
  • seo的网站特征网站的模板
  • 服务器购买网站市场营销策划案模板
  • 网站后台管理系统域名做废品回收哪个网站好点
  • hugo 怎么做网站站酷网首页
  • 深圳福田网站建设公司公司变更证明模板
  • 大连开发区网站设计公司最新国际军事动态和军事战争
  • 住房城乡建设厅网站准考证什么做的网站
  • 福建住房和城乡建设部网站紧急紧急626969
  • 中文域名有哪些网站中铁三局招聘身材好
  • 国内做外贸网站的有哪些能自己做谱子的网站
  • 域名备案掉了网站还可以用263企业邮箱注册入口
  • 注册营业执照网站地情网站建设总结
  • 需要注册的网站建设郑州企业管理咨询有限公司
  • 商业门户网站怎么运营百度推广怎么收费标准案例
  • 企业网站建设方案及报价中国建设银行网站首页签约
  • 做任务网站建设网页设计总结分析