LayoutInflater和inflate()方法的用法
LayoutInflater作用是将layout的xml布局文件实例化为View类对象。
实现LayoutInflater的实例化共有3种方法,
(1).通过SystemService获得
LayoutInflaterinflater = (LayoutInflater)context.getSystemServices(Context.LAYOUT_INFLATER_SERVICES); Viewview = inflater.inflate(R.layout.main, null);
(2).从给定的context中获得
LayoutInflaterinflater = LayoutInflater.from(context);
Viewview = inflater.inflate(R.layout.mian, null);
(3).
LayoutInflaterinflater =getLayoutInflater();//(在Activity中可以使用,实际上是View子类下window的一个函数) Viewlayout = inflater.inflate(R.layout.main, null);
其实,这三种方式本质是相同的,从源码中可以看出:
getLayoutInflater():
Activity的getLayoutInflater()方法是调用PhoneWindow的getLayoutInflater()方法,看一下该源代码:
publicPhoneWindow(Contextcontext) {
super(context);
mLayoutInflater= LayoutInflater.from(context);
}
可以看出它其实是调用LayoutInflater.from(context)。
LayoutInflater.from(context):
public static LayoutInflaterfrom(Context context) {
LayoutInflaterLayoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if(LayoutInflater== null){
thrownew AssertionError("LayoutInflaternot found.");
}
returnLayoutInflater;
}
可以看出它其实调用context.getSystemService()。
public View inflate(int Resourece,ViewGrouproot)
作用:填充一个新的视图层次结构从指定的XML资源文件中
reSource:View的layout的ID
root: 生成的层次结构的根视图
return 填充的层次结构的根视图。如果参数root提供了,那么root就是根视图;否则填充的XML文件的根就是根视图。
其余几个重载的inflate函数类似。
LayoutInflater和inflate()方法的用法的更多相关文章
- android LayoutInflater和inflate()方法的用法(转载)
原文出处:http://www.cnblogs.com/top5/archive/2012/05/04/2482328.html 在实际开发中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,我今天想通过三个案例来让小伙伴彻底的搞清楚这个东东 ...
- View.inflate和LayoutInflater的inflate方法区别
平时ListView加载item中,adapter的getView方法中,我们经常用到: LayoutInflater.from(mContext).inflate(R.layout.it ,pare ...
- 【转】三个案例带你看懂LayoutInflater中inflate方法两个参数和三个参数的区别
关于inflate参数问题,我想很多人多多少少都了解一点,网上也有很多关于这方面介绍的文章,但是枯燥的理论或者翻译让很多小伙伴看完之后还是一脸懵逼,so,我今天想通过三个案例来让小伙伴彻底的搞清楚这个 ...
- 【Android 界面效果43】Android LayoutInflater的inflate方法中attachToRoot的作用
我们在ListView的Adapter的getView方法里面经常会调用两个参数的inflate方法, mInflater.inflate(R.layout.adv_viewpager, null); ...
随机推荐
- python 中类方法@classmethod
classmethod是用来指定一个类的方法为类方法,没有此参数指定的类的方法为实例方法,使用方法如下: class C: @classmethod def f(cls, arg1, arg2, .. ...
- C# 文件流相关操作
二进制转换成图片: MemoryStream ms = new MemoryStream(bytes); ms.Position = ; Image img = Image.FromStream(ms ...
- linux 查看是否安装perl模块
这里介绍两种linux中查看perl模块是否安装的方法,一种是对于单体的模块,一种是对于群体的. 单体验证: [root@root ~]# perl -MShell -e "print\&q ...
- servlet文件上传
1.获取文件上传路径 String path=request.getServletContext.getRePath("/WEB-INF/resources"); 2.获得工厂 3 ...
- Java中HashMap案例
package ch8; import java.util.*; /** * Created by Jiqing on 2016/11/27. */ public class MapTest { pu ...
- Swift语言学习之学习资源
(1) http://swift.sh (2) Let's Swift – WRITE THE CODE. CHANGE THE WORLD. http://letsswift.com (3)http ...
- OpenGL的gluLookAt和glOrtho的关系
OpenGL的gluLookAt和glOrtho的关系 一直不明白gluLookAt()和glOrtho()两者之间的关系:gluLookAt()是观察变换,glOrtho()是正交投影.glLook ...
- Redhat 6.4_联网 yum 配置
步骤简述----------------------------------------------------------------- 1. 准备软件包 下载第三步的软件包即可.由于是联网yum配 ...
- 是不是每个新手都要在samba那儿交足够的时间学费?
每次重新装了虚拟机都会在安装几个必要的服务器这个浪费不少时间, 这一次在Fedora20上装samba,这么配置都无法再windows上登陆,查看了各种配置文件,折腾的天昏地暗,最后终于找到原因: 在 ...
- iOS开发 画六边形(多边形)
- (void)drawRect:(CGRect)rect { UIBezierPath * path = [UIBezierPath bezierPath]; [path moveToPoint:C ...