平时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,也是用这种方式:

@Override
public 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);

View.inflate和LayoutInflater的inflate方法区别的更多相关文章

  1. 转载 LayoutInflater的inflate函数用法详解

    http://www.open-open.com/lib/view/open1328837587484.html LayoutInflater的inflate函数用法详解 LayoutInflater ...

  2. 三个案例带你看懂LayoutInflater中inflate方法两个参数和三个参数的区别

    关于inflate参数问题,我想很多人多多少少都了解一点,网上也有很多关于这方面介绍的文章,但是枯燥的理论或者翻译让很多小伙伴看完之后还是一脸懵逼,so,我今天想通过三个案例来让小伙伴彻底的搞清楚这个 ...

  3. Android编程之LayoutInflater的inflate方法具体解释

    LayoutInflater的inflate方法,在fragment的onCreateView方法中经经常使用到: public View onCreateView(LayoutInflater in ...

  4. Android编程之LayoutInflater的inflate方法实例

    假设你不关心其内部实现,仅仅看怎样使用的话,直接看这篇就可以. 接上篇,接下来,就用最最简单的样例来说明一下: 用两个布局文件main 和 test: 当中,main.xml文件为: <?xml ...

  5. Android编程之LayoutInflater的inflate方法详解

    LayoutInflater的inflate方法,在fragment的onCreateView方法中经常用到: public View onCreateView(LayoutInflater infl ...

  6. 带你看懂LayoutInflater中inflate方法

    关于inflate问题,我想很多人多多少少都了解一点,网上也有很多关于这方面介绍的文章,但是枯燥的理论或者翻译让很多小伙伴看完之后还是一脸懵逼,so,我今天想通过三个案例来让小伙伴彻底的搞清楚这个东东 ...

  7. 转载《 LayoutInflater 的inflate函数用法详解》

    很多人在网上问LayoutInflater类的用法,以及inflate()方法参数的含义,现解释如下: inflate()的作用就是将一个用xml定义的布局文件查找出来,注意与findViewById ...

  8. LayoutInflater和inflate()

    LayoutInflater LayoutInflater抽象类是用来加载XML布局文件(UI界面)的. 作用: 1.对于一个没有被载入或者想要动态载入的界面,都需要使用LayoutInflater. ...

  9. LayoutInflater 与 inflate

    Instantiates a layout XML file into itscorresponding View objects. LayoutInflater作用是将layout的xml布局文件实 ...

随机推荐

  1. Android 字符乱码问题的处理

    <Android 网络HTML查看器>一文中,运行代码实践一下 发现html源代码中出现了乱码,原因很明显:charset="gb2312" android默认的字符集 ...

  2. 优化MySchool数据库(二)

    优化School数据库(TSQL建库建表建约束) 使用T_sql代码建库.建表.建约束: 建库: Create database HotelManagerSystem on ( ---- 数据文件-- ...

  3. Android IPC机制之ContentProvider

    ContentProvider:即内容提供者,用来管理数据,并对外暴露一个uri,外部可以通过uri和数据建立联系并获取或操作数据: 服务端:1.首先创建一个数据库类,并创建一个表:2.创建一个Con ...

  4. UITextField 的限制输入金额(可为小数的正确金额)

    要判断输入金额为正确金额的方法有两个,一个是用正则表达式,另一个就是用textfield的代理方法 有时候难免遇到这样的需求,不符合规则的金额就不让输入时,那用这种方法比较合理 如果设置输入键盘为De ...

  5. vs 只能没有智能提示的解决方法

    我vs今天出现一点比较诡异的情况,莫名奇妙就不能自动生成我拖的控件的后端代码了(****.aspx.designer.cs) 我在网上找了下,找到一个解决方法,这里把解决方法贴下来. 网上解决方法的出 ...

  6. 学习 Docker - 入门

    Docker简介 一种虚拟容器技术. 一种虚拟化分方案: 操作系统级别的虚拟化: 只能运行相同或相似内核的操作系统: 依赖与linux内核特性:Namespace和Cgroups(Control Gr ...

  7. Apache 反向代理实现为http添加https的外衣

    Apache 反向代理 金天:坚持写东西,不是一件容易的事,换句话说其实坚持本身都不是一件容易的事.如果学习有捷径,那就是不断实践,不断积累.写笔记,其实是给自己看的,是体现积累的一种方式,要坚持. ...

  8. Java代码规范

    Java代码规范 本Java代码规范以SUN的标准Java代码规范为基础,为适应我们公司的实际需要,可能会做一些修改.本文档中没有说明的地方,请参看SUN Java标准代码规范.如果两边有冲突,以SU ...

  9. 【shell--批量远程MySQL,执行命令】-【工作总结】

    昨天下班前,老板给了一批LOG数据库IP地址,需要统计LOG表里Message字段top 10的结果,并输出到一个excel文件里.抽查看了下,有两种格式的以当天日期结尾的表名.由于数量太多,时间紧迫 ...

  10. mkfifo

    管道是Linux的十种文件类型之一,使用管道通信本质上还是以文件作为通信的媒介 有名管道+无名管道=管道 有名管道(FIFO文件):就是 有文件名的管道, 可以用于任意两个进程间的通信 无名管道(pi ...