View.inflate和LayoutInflater的inflate方法区别
平时ListView加载item中,adapter的getView方法中,我们经常用到:
LayoutInflater.from(mContext).inflate(R.layout.it ,parent,false);
这样的方法来加载布局xml,平时一直就是这么用的,也没什么疑问。今天网上看了个自定义布局的源码,自定义布局中加载布局xml用的View.inflate方法:
public class SettingItemView extends RelativeLayout { private CheckBox cb_status; private TextView tv_description; private TextView tv_title; private String desc_on; private String desc_off; private void iniView(Context context) { View.inflate(context, R.layout.setting_item_view, this);//第三个参数传布局文件的父类 cb_status=(CheckBox) this.findViewById(R.id.cb_status); tv_description=(TextView) this.findViewById(R.id.tv_description); tv_title=(TextView) this.findViewById(R.id.tv_title); }
第一次见用这种方式来加载布局的,看了下他的listview加载item,也是用这种方式:
@Overridepublic View getView(final int position, View convertView, ViewGroup parent) {View view;ViewHolder holder;if(convertView==null){view=View.inflate(getApplicationContext(), R.layout.list_item_callsms, null);//最后一个传了null holder=new ViewHolder(); holder.tv_number=(TextView) view.findViewById(R.id.tv_black_number); holder.tv_mode=(TextView) view.findViewById(R.id.tv_black_mode); holder.iv_delete=(ImageView) view.findViewById(R.id.iv_delete); view.setTag(holder);
好吧,看一下View.inflate的说明:
Open Declaration View android.view.View.inflate(Context context, int resource, ViewGroup root)Inflate a view from an XML resource. This convenience method wraps the
LayoutInflater class, which provides a full range of options for view
inflation.Parameters: context The Context object for your activity or
application. resource The resource ID to inflate root A view group
that will be the parent. Used to properly inflate the layout_*
parameters.
See Also: LayoutInflater
最后有一句让你看LayoutInflater这个类,怀疑它内部也是用LayoutInflater实现的,进入源码:public static View inflate(Context context, int resource, ViewGroup root) {LayoutInflater factory = LayoutInflater.from(context); return factory.inflate(resource, root); }
果然内部也是用LayoutInflater实现的,不知道为啥android还要用View.inflat封装一下。。。o(〃’▽’〃)o
其中LayoutInflater的Inflate的三个参数意思为:对于Inflate的三个参数(int resource, ViewGroup root, boolean attachToRoot)
如果inflate(layoutId, null )则layoutId的最外层的控件的宽高是没有效果的
如果inflate(layoutId, root, false ) 则认为和上面效果是一样的
如果inflate(layoutId, root, true ) 则认为这样的话layoutId的最外层控件的宽高才能正常显示
对这三个参数区别不理解的话可以看这篇文章:
inflate第三个参数意思从源码角度解析的有郭大神的:
Android LayoutInflater原理分析,带你一步步深入了解View(一)
以及另一篇感觉很不错的:
Android LayoutInflate深度解析 给你带来全新的认识看完,你应该知道这个参数意思了,ok,再来看上面代码, 这时就可以替换为layoutInflater的方式了:
对于第一个自定义布局:
//View.inflate(context, R.layout.setting_item_view, this);//第三个参数传布局文件的父类LayoutInflater.from(context).inflate(R.layout.setting_item_view, this, true);//等价于上面
第二个适配器中getView:
//view=View.inflate(getApplicationContext(), R.layout.list_item_callsms, null);view=LayoutInflater.from(getApplicationContext()).inflate(R.layout.list_item_callsms,parent,false);
转载于:https://www.cnblogs.com/ldq2016/p/6244780.html
View.inflate和LayoutInflater的inflate方法区别相关推荐
- 三个案例带你看懂LayoutInflater中inflate方法两个参数和三个参数的区别
本文转载自[http://blog.csdn.net/u012702547/article/details/52628453] 关于inflate参数问题,我想很多人多多少少都了解一点,网上也有很多关 ...
- 带你看懂LayoutInflater中inflate方法
关于inflate问题,我想很多人多多少少都了解一点,网上也有很多关于这方面介绍的文章,但是枯燥的理论或者翻译让很多小伙伴看完之后还是一脸懵逼,so,我今天想通过三个案例来让小伙伴彻底的搞清楚这个东东 ...
- LayoutInflater的inflate方法使用
Android动态添加布局时经常会用到LayoutInflater的inflate(int resource, ViewGroup root, boolean attachToRoot) 方法,但是i ...
- LayoutInflater中inflate()方法的学习使用
LayoutInflater中inflate()方法的学习使用 一.LayoutInflater LayoutInflater实例化一个xml布局加到对应的View对象中.该对象不直接使用.调用g ...
- LayoutInflater的inflate函数用法详解
LayoutInflater的inflate函数用法详解 LayoutInflater作用是将layout的xml布局文件实例化为View类对象. 获取LayoutInflater的方法有如下三种: ...
- View的测量规则以及三大方法流程
通过前面几篇的深入分析,相信大家对View的理解已经很深了,我们说了setContentView背后做了什么,说了View从xml加载到通过WindowManager添加View后的一系列操作,说了A ...
- 浅析对象访问属性的.和[]方法区别
原文链接:http://www.cnblogs.com/bigboyLin/p/4967820.html 简明版:请问js对象属性值为什么用数组也可以访问 在JavaScript中通常使用" ...
- 无法解析 list 中的方法 iterator_Python-list中的append()和extend()方法区别
一.append()和extend()方法都是用来添加数据到list末尾的,两者的区别: append()添加的时候会把添加的数据当成一个整体进行添加,允许添加任意类型的数据 extend()添加的时 ...
- android中实现view的更新有两组方法
android中实现view的更新有两组方法,一组是invalidate,另一组是postInvalidate,其中前者是在UI线程自身中使用,而后者在非UI线程中使用. Android提供了 ...
最新文章
- 200 个工具分析机器学习十年:开源是大势,工程师是核心
- python关闭读写的所有的文件-Python文件操作:文件的打开关闭读取写入
- The Property System
- NUMA架构和Java
- 利用VMware Infrastructure SDK编程控制虚拟机集群(3)
- oracle查询两个分区,oracle下 分区表部分分区查询慢有关问题(2)
- 07_Python的控制判断循环语句1(if判断for循环)_Python编程之路
- 谷歌插件IDM下载MODIS数据
- 大数据集群治理与数据治理(PPT)
- Excel自定义格式日期计算问题
- 化学式中间的点怎么打?
- python实用脚本(三)—— 通过有道智云API实现翻译
- wh计算公式_锂电池计算公式wh 锂电池的mah和wh如何换算?
- sublime text 提示error while loading pyvb binvry:exit code3 try tomanually install pyvb的处理方法
- 802.11速率详解
- python模糊图像判断
- kubectl k8s 复制文件从宿主机到pod内或从pod到宿主机内
- 树状数组原理及经典应用问题
- 苹果手机有护眼模式吗_睡觉前忍不住刷手机?除了夜间模式,这些护眼小技巧赶快收好!...
- python接口自动化-token
热门文章
- hdu5698瞬间移动(组合数,逆元)
- 在IntelliJ IDEA中添加repository模板
- JavaScript frame跨域获取元素、修改元素属性、调用其他frame页面方法
- 《你必须知道的261个java语言问题》读书报告——第二弹
- 生活随笔:态度需要端正
- 多文件目录下makefile文件递归执行编译所有c文件 很不错
- 用计算机答题答案提交后能否改错,南京晓庄计算机操作系统习题库含答案全1-5章...
- 函数 单片机glint_GL 库函数
- js map 排序_数组方法写给女友的一系列 JS 数组操作(建议收藏 | 内附思维导图)...
- java 反射 私有成员_Java对类私有变量的暴力反射技术讲解