Android LayoutInflater及inflate方法
2016-04-10 15:50
369 查看
相信大家对LayoutInflate都不陌生,特别在ListView的Adapter的getView方法中基本都会出现,使用inflate方法去加载一个布局,用于ListView的每个Item的布局。
LayoutInflater的作用类似于 findViewById(),不同点是LayoutInflater是用来找layout文件夹下的xml布局文件,并且实例化;而
findViewById()是找具体某一个xml下的具体 widget控件(如:Button,TextView等)。
具体区别:
对于一个没有被载入或者想要动态载入的界面,都需要使用LayoutInflater.inflate()来载入;
对于一个已经载入的界面,就可以使用Activiyt.findViewById()方法来获得其中的界面元素
LayoutInflater 是一个抽象类,在文档中如下声明:
public abstract class LayoutInflater extends Object
一、获得 LayoutInflater 实例的三种方式
方式1. 由LayoutInflater的静态函数 from(Context context) 获取:
static LayoutInflater from(Context context);
方式2. 由服务获取:
二、调用inflate()方法
Inflate有三个参数(int resource, ViewGroup root, boolean attachToRoot)
作用:从指定的XML资源文件中填充一个新的视图层次结构
三个参数:
resource: 布局的资源id
root: 填充的根视图
attachToRoot: 是否将载入的视图绑定到根视图中
这个方法重载了四种调用方式,分别为:
public View inflate(int resource, ViewGroup root)
public View inflate(int resource, ViewGroup root, boolean attachToRoot)
public View inflate(XmlPullParser parser, ViewGroup root)
public View inflate(XmlPullParser parser, ViewGroup root, boolean attachToRoot)
这四种使用方式中,我们最常用的是第一种方式,inflate方法的主要作用就是将xml转换成一个View对象,用于动态的创建布局。虽然重载了四个方法,但是这四种方法最终调用的,还是第四种方式。第四种方式也很好理解,内部实现原理就是利用Pull解析器,对Xml文件进行解析,然后返回View对象。
具体例子请参考郭霖大神的博客:Android LayoutInflater原理分析,带你一步步深入了解View(一)
LayoutInflater的作用类似于 findViewById(),不同点是LayoutInflater是用来找layout文件夹下的xml布局文件,并且实例化;而
findViewById()是找具体某一个xml下的具体 widget控件(如:Button,TextView等)。
具体区别:
对于一个没有被载入或者想要动态载入的界面,都需要使用LayoutInflater.inflate()来载入;
对于一个已经载入的界面,就可以使用Activiyt.findViewById()方法来获得其中的界面元素
LayoutInflater 是一个抽象类,在文档中如下声明:
public abstract class LayoutInflater extends Object
一、获得 LayoutInflater 实例的三种方式
方式1. 由LayoutInflater的静态函数 from(Context context) 获取:
static LayoutInflater from(Context context);
LayoutInflater inflater = LayoutInflater.from(this); View view=inflater.inflate(R.layout.ID, null);
方式2. 由服务获取:
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);方式3. 调用Activity的getLayoutInflater() 函数获取LayoutInflater 对象:
二、调用inflate()方法
Inflate有三个参数(int resource, ViewGroup root, boolean attachToRoot)
作用:从指定的XML资源文件中填充一个新的视图层次结构
三个参数:
resource: 布局的资源id
root: 填充的根视图
attachToRoot: 是否将载入的视图绑定到根视图中
这个方法重载了四种调用方式,分别为:
public View inflate(int resource, ViewGroup root)
public View inflate(int resource, ViewGroup root, boolean attachToRoot)
public View inflate(XmlPullParser parser, ViewGroup root)
public View inflate(XmlPullParser parser, ViewGroup root, boolean attachToRoot)
这四种使用方式中,我们最常用的是第一种方式,inflate方法的主要作用就是将xml转换成一个View对象,用于动态的创建布局。虽然重载了四个方法,但是这四种方法最终调用的,还是第四种方式。第四种方式也很好理解,内部实现原理就是利用Pull解析器,对Xml文件进行解析,然后返回View对象。
具体例子请参考郭霖大神的博客:Android LayoutInflater原理分析,带你一步步深入了解View(一)
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件
- SourceProvider.getJniDirectories