Android获取LayoutInflater对象的方法总结
2014-05-23 10:48
453 查看
在写Android程序时,有时候会编写自定义的View,使用Inflater对象来将布局文件解析成一个View。本文主要目的是总结获取LayoutInflater对象的方法。
1、若能获取context对象,可以有以下几种方法:
[java] view plaincopy
![](https://code.csdn.net/assets/CODE_ico.png)
![](https://code.csdn.net/assets/ico_fork.svg)
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View child = inflater.inflate(R.layout.child, null);
or
[java] view plaincopy
![](https://code.csdn.net/assets/CODE_ico.png)
![](https://code.csdn.net/assets/ico_fork.svg)
LayoutInflater inflater = LayoutInflater.from(context);
View child = inflater.inflate(R.layout.child, null);
[java] view plaincopy
![](https://code.csdn.net/assets/CODE_ico.png)
![](https://code.csdn.net/assets/ico_fork.svg)
</pre><p></p><pre>
2、在一个Activity中,可以有以下方法:
[java] view plaincopy
![](https://code.csdn.net/assets/CODE_ico.png)
![](https://code.csdn.net/assets/ico_fork.svg)
View child = getLayoutInflater().inflate(R.layout.child, item, false);
or
[java] view plaincopy
![](https://code.csdn.net/assets/CODE_ico.png)
![](https://code.csdn.net/assets/ico_fork.svg)
View view;
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.mylayout, null);
方法1和方法2其实都是对context().getSystemService()的使用
3、使用View的静态方法:
[java] view plaincopy
![](https://code.csdn.net/assets/CODE_ico.png)
![](https://code.csdn.net/assets/ico_fork.svg)
View view=View.inflate(context, R.layout.child, null)
inflate实现源码如下:
[java] view plaincopy
![](https://code.csdn.net/assets/CODE_ico.png)
![](https://code.csdn.net/assets/ico_fork.svg)
/**
* Inflate a view from an XML resource. This convenience method wraps the {@link
* LayoutInflater} class, which provides a full range of options for view inflation.
*
* @param context The Context object for your activity or application.
* @param resource The resource ID to inflate
* @param root A view group that will be the parent. Used to properly inflate the
* layout_* parameters.
* @see LayoutInflater
*/
public static View inflate(Context context, int resource, ViewGroup root) {
LayoutInflater factory = LayoutInflater.from(context);
return factory.inflate(resource, root);
}
LayoutInflater.from(context)实际上是对方法1的包装,可参考以下源码:
[java] view plaincopy
![](https://code.csdn.net/assets/CODE_ico.png)
![](https://code.csdn.net/assets/ico_fork.svg)
/**
* Obtains the LayoutInflater from the given context.
*/
public static LayoutInflater from(Context context) {
LayoutInflater LayoutInflater =
(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (LayoutInflater == null) {
throw new AssertionError("LayoutInflater not found.");
}
return LayoutInflater;
}
1、若能获取context对象,可以有以下几种方法:
[java] view plaincopy
![](https://code.csdn.net/assets/CODE_ico.png)
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View child = inflater.inflate(R.layout.child, null);
or
[java] view plaincopy
![](https://code.csdn.net/assets/CODE_ico.png)
LayoutInflater inflater = LayoutInflater.from(context);
View child = inflater.inflate(R.layout.child, null);
[java] view plaincopy
![](https://code.csdn.net/assets/CODE_ico.png)
</pre><p></p><pre>
2、在一个Activity中,可以有以下方法:
[java] view plaincopy
![](https://code.csdn.net/assets/CODE_ico.png)
View child = getLayoutInflater().inflate(R.layout.child, item, false);
or
[java] view plaincopy
![](https://code.csdn.net/assets/CODE_ico.png)
View view;
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.mylayout, null);
方法1和方法2其实都是对context().getSystemService()的使用
3、使用View的静态方法:
[java] view plaincopy
![](https://code.csdn.net/assets/CODE_ico.png)
View view=View.inflate(context, R.layout.child, null)
inflate实现源码如下:
[java] view plaincopy
![](https://code.csdn.net/assets/CODE_ico.png)
/**
* Inflate a view from an XML resource. This convenience method wraps the {@link
* LayoutInflater} class, which provides a full range of options for view inflation.
*
* @param context The Context object for your activity or application.
* @param resource The resource ID to inflate
* @param root A view group that will be the parent. Used to properly inflate the
* layout_* parameters.
* @see LayoutInflater
*/
public static View inflate(Context context, int resource, ViewGroup root) {
LayoutInflater factory = LayoutInflater.from(context);
return factory.inflate(resource, root);
}
LayoutInflater.from(context)实际上是对方法1的包装,可参考以下源码:
[java] view plaincopy
![](https://code.csdn.net/assets/CODE_ico.png)
/**
* Obtains the LayoutInflater from the given context.
*/
public static LayoutInflater from(Context context) {
LayoutInflater LayoutInflater =
(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (LayoutInflater == null) {
throw new AssertionError("LayoutInflater not found.");
}
return LayoutInflater;
}
相关文章推荐
- Android开发之获取LayoutInflater对象的方法总结
- Android获取LayoutInflater对象的方法总结
- Android获取LayoutInflater对象的方法总结
- Android获取LayoutInflater对象的方法总结
- 获取LayoutInflater对象的方法和inflate方法的一些参数问题
- Android获取LayoutInflater的一种方法
- 获取LayoutInflater对象三种方法
- 获取LayoutInflater对象的方法和inflate方法的一些参数问题
- Android中获取LayoutInflater的三种方法以及区别
- android初级之Android获取网页数据的方法总结
- dojo获取组件(组件对象)的方法总结
- Android获取网页数据的方法总结
- Android 漫游之路------ListView入门、Layout的Inflater创建View对象、常用数据适配器
- Android获取内存的不同方法总结
- Android学习 之 获取可视区域的Rect对象(顺带获取状态栏和标题栏高度的方法)
- 获取LayoutInflater的三种方法
- Android获取系统时间方法的总结
- event对象获取方法总结在google浏览器下测试
- 获取LayoutInflater的三种方法
- Android学习 之 获取可视区域的Rect对象(顺带获取状态栏和标题栏高度的方法)