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,也是用这种方式:
@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方法区别的更多相关文章
- 转载 LayoutInflater的inflate函数用法详解
http://www.open-open.com/lib/view/open1328837587484.html LayoutInflater的inflate函数用法详解 LayoutInflater ...
- 三个案例带你看懂LayoutInflater中inflate方法两个参数和三个参数的区别
关于inflate参数问题,我想很多人多多少少都了解一点,网上也有很多关于这方面介绍的文章,但是枯燥的理论或者翻译让很多小伙伴看完之后还是一脸懵逼,so,我今天想通过三个案例来让小伙伴彻底的搞清楚这个 ...
- Android编程之LayoutInflater的inflate方法具体解释
LayoutInflater的inflate方法,在fragment的onCreateView方法中经经常使用到: public View onCreateView(LayoutInflater in ...
- Android编程之LayoutInflater的inflate方法实例
假设你不关心其内部实现,仅仅看怎样使用的话,直接看这篇就可以. 接上篇,接下来,就用最最简单的样例来说明一下: 用两个布局文件main 和 test: 当中,main.xml文件为: <?xml ...
- Android编程之LayoutInflater的inflate方法详解
LayoutInflater的inflate方法,在fragment的onCreateView方法中经常用到: public View onCreateView(LayoutInflater infl ...
- 带你看懂LayoutInflater中inflate方法
关于inflate问题,我想很多人多多少少都了解一点,网上也有很多关于这方面介绍的文章,但是枯燥的理论或者翻译让很多小伙伴看完之后还是一脸懵逼,so,我今天想通过三个案例来让小伙伴彻底的搞清楚这个东东 ...
- 转载《 LayoutInflater 的inflate函数用法详解》
很多人在网上问LayoutInflater类的用法,以及inflate()方法参数的含义,现解释如下: inflate()的作用就是将一个用xml定义的布局文件查找出来,注意与findViewById ...
- LayoutInflater和inflate()
LayoutInflater LayoutInflater抽象类是用来加载XML布局文件(UI界面)的. 作用: 1.对于一个没有被载入或者想要动态载入的界面,都需要使用LayoutInflater. ...
- LayoutInflater 与 inflate
Instantiates a layout XML file into itscorresponding View objects. LayoutInflater作用是将layout的xml布局文件实 ...
随机推荐
- tomcat通过socket连接MySQL,不再占用服务端口【linux】
MySQL连接方式的说明 http://icbm.iteye.com/blog/1840673 MySQL除了最常见的TCP连接方式外,还提供SOCKET(LINUX默认连接方式).PIPE和SHAR ...
- webbench 压力测试
原文 webbench最多可以模拟3万个并发连接去测试网站的负载能力,个人感觉要比Apache自带的ab压力测试工具好用,安装使用也特别方便,并且非常小. 主要是 -t 参数用着比较爽,下面参考了张宴 ...
- iOS开发UINavigation——导航控制器UINavigationController
iOS开发UINavigation系列一——导航栏UINavigtionBar摘要iOS中的导航条可以附着于导航控制器之中使用,也可以在controller中单独使用,这篇博客,主要讨论有关导航栏的使 ...
- iOS---检测系统通知开关状态
if (iOS8) { //iOS8以上包含iOS8 if ([[UIApplication sharedApplication] currentUserNotificationSettings].t ...
- 【代码笔记】iOS-屏幕旋转
代码: - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. se ...
- OC中runtime的使用
一.runtime简介* RunTime简称运行时.OC就是“运行时机制”,也就是在运行时候的一些机制,其中最主要的是消息机制.* 对于C语言,“函数的调用在编译的时候会决定调用哪个函数”.* 对于O ...
- 控制器View的加载和内存警告流程图
控制器View的加载 内存警告
- eclispe常用快捷键
一个Eclipse骨灰级开发者总结了他认为最有用但又不太为人所知的快捷键组合.通过这些组合可以更加容易的浏览源代码,使得整体的开发效率和质量得到提升. 几个最重要的快捷键 Alt + / 代码助手 C ...
- Discuz! 经典加密解密函数
function authcode($string, $operation = 'DECODE', $key = '', $expiry = 0) { $ckey_length = 4; $key = ...
- HTML5设计网页熔岩灯导航(navigation bar)插件 已经加上完整源代码
导航栏(navigation bar): 1.指位于页眉区域的,在页眉横幅图片上边或下边的一排水平导航按钮,它起着链接博客的各个页面的作用. 2.网页设计中不可缺少的部分,它是指通过一定的技术手段,为 ...