用Fragment很久了,今天忽然发现自己写的东西,明明子Fragment是全屏,但是宽度只有那么一点点。其实这个问题的本质是inflate的方法的使用,之前也研究过但没有留下记录,在fragment使用上又暴漏出来了。直觉告诉我一定是哪块出问题了,很快锁定到onCreateView上。

在onCreateView里我们一般有两种写法:

方法1:

public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {// TODO Auto-generated method stubView root = inflater.inflate(layoutId(), container, false);return root;}

方法2:

public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {// TODO Auto-generated method stubView root = inflater.inflate(layoutId(), null);return root;}   

这两种都可以,但是如果采用第二种,就不会继承到母布局的参数信息。来看下文档:

方法2调用的inflate()方法如下:

/*** Inflate a new view hierarchy from the specified xml resource. Throws* {@link InflateException} if there is an error.* * @param resource ID for an XML layout resource to load (e.g.,*        <code>R.layout.main_page</code>)* @param root Optional view to be the parent of the generated hierarchy.* @return The root View of the inflated hierarchy. If root was supplied,*         this is the root View; otherwise it is the root of the inflated*         XML file.*/public View inflate(int resource, ViewGroup root) {return inflate(resource, root, root != null);}

方法1调用的inflate()方法如下:

 /*** Inflate a new view hierarchy from the specified xml resource. Throws* {@link InflateException} if there is an error.* * @param resource ID for an XML layout resource to load (e.g.,*        <code>R.layout.main_page</code>)* @param root Optional view to be the parent of the generated hierarchy (if*        <em>attachToRoot</em> is true), or else simply an object that*        provides a set of LayoutParams values for root of the returned*        hierarchy (if <em>attachToRoot</em> is false.)* @param attachToRoot Whether the inflated hierarchy should be attached to*        the root parameter? If false, root is only used to create the*        correct subclass of LayoutParams for the root view in the XML.* @return The root View of the inflated hierarchy. If root was supplied and*         attachToRoot is true, this is root; otherwise it is the root of*         the inflated XML file.*/public View inflate(int resource, 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();}}

方法2调用的inflate()方法最终调用了方法1中调用的inflate()方法,即在inflate(int resource, ViewGroup root)中调用了inflate(int resource, ViewGroup root, boolean attachToRoot),所以直接看方法1调用的inflate()方法.

如果root不为空,attachToRoot为false的情况下,文档说的很明白:If false, root is only used to create the correct subclass of LayoutParams for the root view in the XML,即root只用来创建父布局的参数信息。而为true,则添加到父布局。因为我们控制fragment时要手动add/remove等,所以此处attachToRoot一定是false。

再反过来看,如果用了方法2,root一定要传null,此时子view不会得到母view的布局信息,子view里一堆的match_parent也就毫无用途。

总结:90%的情况下,我们使用fragment此处一定要用方法1. 有一种情况,如创建dialog fragment时,不需要得到母布局参数信息可以用方法2.

补充:上文代码里的layoutId()是个虚函数,用来在子类里实现。子类只需return R.layout.xxx就可以了,不必每个fragment都重写onCreateView了。在onViewCreated里根据传来的参数root去实例化各个控件。

原文URL:点击打开链接

Fragment onCreateView inflate注意事项 (整理)相关推荐

  1. Unity 工具 之 CurvedUI (曲面UI)一些基本操作和CUSTOM_RAY 射线交互时注意事项整理

    Unity 工具 之 CurvedUI(曲面UI) 简单使用介绍和CUSTOM_RAY 射线交互时注意事项整理 目录 Unity 工具 之 CurvedUI(曲面UI) 简单使用介绍和CUSTOM_R ...

  2. 英文论文写作注意事项整理

    写英文论文也有两篇了,至少每一篇写完之后都需要有些长进和总结,并且为了在后续的写作中不被老师请去喝茶,所以整理了一下自己在英文写作中经常出现的问题. 语法 单数复数的问题是最不该犯错的,什么时候用is ...

  3. 光盘加密刻录时流程及注意事项整理

    光盘加密刻录 1 背景 2 准备 2.1 电脑硬件 2.2 电脑软件 3 刻录 3.1 WinISO 3.2 光盘加密大师 3.3 Nero 4 总结 1 背景 由于要求,需要将相关材料刻录进光盘中, ...

  4. 合伙开公司应有哪些注意事项(整理贴)

    zhuxuan52 : 我看了注册资金两个人最少三万,普通的小公司,卖点产品 主要是从公司运营的风险.公司的日常管理.利润的分成以及合伙的注意事项方面 可能以后就合伙人两个,没有其他人,公司法.税法. ...

  5. 自己定义微信图文模板注意事项整理

    自己定义模板, 要把css写在html  style中,在网页打开复制整个文章,复制的时候会把样式一起复制进去,粘贴到微信素材管理->后台编辑器中预览就可以实现. 1,html复制到后台编辑器之 ...

  6. 一句话以上题解及注意事项整理已完成的题目(1)

    jdfz.com 1001 A+B 1006 pla 一维DP+单调栈优化 1007 美食家 贪心策略,尽可能多选 1008 牛排序 二分查找+贪心策略 1009 护花 贪心策略,每次移动y/x最大的 ...

  7. STM32的CAN开发注意事项整理收集

    一.STM32的bxCAN控制器 bxCAN是基本扩展CAN(Basic Extended CAN)的缩写,它支持CAN协议2.0A和2.0B.它的设计目标是,以最小的CPU负荷来高效处理大量收到的报 ...

  8. 关于Fragment的setUserVisibleHint() 方法和onCreateView()的执行顺序

    1:setUserVisibleHint(boolean isVisibleToUser)的方法就很重要,根据方法名来看当前页面是否可见, 里面的boolean值就是判断当前页面是否可见的变量,所以大 ...

  9. Fragment的onCreateView创建的view是如何加入到Activity的

    ** Fragment的onCreateView创建的view是如果加入到Activity的过程分析 ** 1.简单介绍一下fragment的使用 在activity的布局里添加一个ViewGroup ...

最新文章

  1. 2022-2028中国曝光机市场现状及未来发展趋势
  2. iOS使用多线程提高数据并发访问 之七
  3. python中的数据类型和常用运算符
  4. Thinkpad上安装Ubuntu相关事项
  5. Dos下删除(非)空目录或文件
  6. grid - 隐式命名网格线名称
  7. redhat下使用mrtg监控主机流量
  8. 取代Cookie失败,Google上线FLoC反遭“围攻”!
  9. 写给创业者的四句金玉良言
  10. 各种有用、有趣网站整理
  11. 【吐血整理】超全golang面试题合集+golang学习指南+golang知识图谱+成长路线 一份涵盖大部分golang程序员所需要掌握的核心知识。
  12. 个人带领团队做过的事
  13. python编写收入支出明细程序_PHP编程之收入支出明细表实现技术
  14. 【Udacity】数据的差异性:值域、IQR、方差和标准差
  15. nodeclub迁移至nodebb
  16. 移动app需求分析与用例设计
  17. 设计师常用的10种平面设计风格,你知道几个?
  18. 探索多层次内存系统的页面管理设计空间Exploring the Design Space of Page Management for Multi-Tiered Memory Systems
  19. docker-compose up:ERROR: Encountered errors while bringing up the project.报错问题
  20. SpringBoot响应式编程(整理一)

热门文章

  1. 19、任务十八——事件委托、数组处理
  2. ionic angularJS input 相关指令 以及定时器 的使用
  3. VS2015中DataGridView的DataGridViewComBoboxCell列值无效及数据绑定错误的解决方法
  4. 清橙 A1120 拦截导弹 -- 动态规划(最长上升子序列)
  5. 外企面试官最爱提的10个问题
  6. 【css】常用的几种水平垂直居中方式与盒子模型,面试经常问到!
  7. 用java实现 两个工具和一个copy功能
  8. HTML 传输 字符转换 进制转换 15位 18位 时间戳 转换
  9. bootstrap源码分析之form、navbar
  10. Java Web编程的主要组件技术——Hibernate入门