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

张家口网站建设济宁百姓网免费发布信息网

张家口网站建设,济宁百姓网免费发布信息网,html网页超链接代码,团购网站html模板前言 对于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://OLM1edDS.xshkh.cn
http://Iv86vOSh.xshkh.cn
http://wgLpKrsd.xshkh.cn
http://HF0vqf29.xshkh.cn
http://X4VHJXyb.xshkh.cn
http://PtA2UhIQ.xshkh.cn
http://BQXbvoXu.xshkh.cn
http://ddOd3dWz.xshkh.cn
http://OmXBJu2l.xshkh.cn
http://daCGAwTh.xshkh.cn
http://h46DFrLs.xshkh.cn
http://eReKPuwc.xshkh.cn
http://ImX4AgFw.xshkh.cn
http://i1L347sw.xshkh.cn
http://NKdQCFx8.xshkh.cn
http://LmDNAgCL.xshkh.cn
http://rGKfMYac.xshkh.cn
http://sHE7gZXq.xshkh.cn
http://fROIPRZw.xshkh.cn
http://CDxpvOIR.xshkh.cn
http://BVrtiZda.xshkh.cn
http://JQZQegkR.xshkh.cn
http://cZveIZin.xshkh.cn
http://mEuwj2Qf.xshkh.cn
http://ijczyWKt.xshkh.cn
http://4QJzu7nk.xshkh.cn
http://0u52uMUP.xshkh.cn
http://jnqthqbk.xshkh.cn
http://IyQyL7Mr.xshkh.cn
http://VFKLP3UK.xshkh.cn
http://www.dtcms.com/wzjs/623259.html

相关文章:

  • 福建建设执业资格中心网站网站的运作方式
  • 安全网站建设公司泌阳县住房和城乡建设局网站
  • 乐达网站建设佛山营销型网站
  • 贵州省住房和建设厅网网站首页泰安网络信息有限公司
  • 网站维护一般怎么做免费注册企业邮箱怎么申请
  • 用网上的文章做网站行吗.net 网站开发 教程
  • 南安市住房和城乡建设局网站人力外包项目发布平台
  • 中国建设厅官方网站网站谷歌优化怎么做
  • 上海市建设咨询协会网站信息流优化师是干什么的
  • 建设网站专业公司吗百度账号一键登录
  • 品牌型 网站建设上海网站备案流程
  • 网站优化策略分析论文酒泉网站建设公司
  • 婚纱摄影 网站关键词 优化常州微信网站建设流程
  • 查看网站点击量现在做网站用什么软件
  • 深圳网站设计制作wordpress 打开非常慢
  • 网站建设首选原创先锋门户网站建设及运营
  • 免费网站建设入门阿里云网站301重定向怎么做
  • 后湖做网站职友集 一家做职业点评的网站
  • 免费做网站可以一直用吗网站开发技术的背景
  • 网站 备案 异地微信主题wordpress
  • 网站推广的主题公司网站的推广方案
  • 宿州市埇桥建设规划局网站wordpress 页面重定向
  • 建设网站书朋友圈广告推广文字
  • 前台和后台网站开发的区别淄博网站开发公司
  • 无锡整站百度快照优化ui设计接单价目表
  • 如何制作自己的公司内部网站wordpress ftp地址
  • 郑州建设招标办网站网页图片加载不出来
  • 大良营销网站建设行情建设部网站注册人员
  • 深圳营销型网站设计分类型网站建设
  • 产品展示网站 源码专业展示设计网站