5665
- 收藏
- 点赞
- 分享
- 举报
安卓中LayoutInflater类及其中inflate方法使用
本帖最后由 jp1017 于 2015-9-11 13:28 编辑
ListView分批加载这个例子里有好多内容,这里先介绍下LayoutInflater类及其中的inflate方法的使用。
一、LayoutInflater类:

可以看到这是一个抽象类,他的作用是把layout的xml布局文件生成对应的View对象,并实例化,这个类的作用类似于findViewById(), 不同点是:
* LayoutInflater是用来找layout下xml布局文件的,而且它会实例化
* findViewById()是找具体xml布局文件下的具体widget控件,比如:Button按钮
该类的实例化方法有三种:
1、该类的from方法,从给定的context中获得
[code]LayoutInflater inflater = LayoutInflater.from(context);
Viewview = inflater.inflate(R.layout.mian, null);[/code]
2、通过SystemService获得,api里也是这么举例的
[code] LayoutInflater inflater = (LayoutInflater)context.getSystemServices(Context.LAYOUT_INFLATER_SERVICES);
View view = inflater.inflate(R.layout.main, null);[/code]
3、通过View类下子类window的一个函数getLayoutInflater()
[code]View layout = inflater.inflate(R.layout.main, null);[/code]
其实,这三个方法本质一样,在底层都是是用到了getSystemServices,
Activity的getLayoutInflater()方法是调用PhoneWindow的getLayoutInflater()方法,(刚才花了一个小时时间找PhoneWindow类,api里没有,感谢网友吧)看一下该源代码:
[code]public PhoneWindow(Contextcontext) {
super(context);
mLayoutInflater= LayoutInflater.from(context);
}[/code]
可以看出它其实是调用LayoutInflater.from(context)。
在来看下LayoutInflater.from(context)源码:
[code]public static LayoutInflater from(Context context) {
LayoutInflater LayoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if(LayoutInflater== null){
throw new AssertionError("LayoutInflaternot found.");
}
return LayoutInflater;
}[/code]
可以看出它其实调用context.getSystemService()。
ListView分批加载用到的就是第三个实例化方法,在activity里实例化页脚,然后把页脚上“正在加载”动画加到ListView上。
二、inflate方法:

可以看到该方法有四种重载形式,这里用的第一个方法,第一个参数resource是想要用的布局文件的id,第二个参数root是持有选项卡的内容,获取FrameLayout ,其他方法类似。
下面是网上的:
inflate就相当于将一个xml中定义的布局找出来.
* 因为如果在一个Activity文件里直接用findViewById()这个方法的话,
* 那么它所对应的是setConentView()中调用的那个layout里的组件.
* 因此如果在同样的Activity里用到别的layout的话,
* 而且你还要设置这个layout里的组件(比如:ImageView,TextView)上的内容,
* 那么你就必须用inflate()先将这个layout找出来, 然后再用这个layout对象去找到它上面的组件
* 然后进行一系列的操作
一句话,这个inflate就是个中介,你这个activity要用别的布局里的组件,你就要先找到这个布局,inflate就是这个角色了。
好了,就是这个样子。enjoy!!!
ListView分批加载这个例子里有好多内容,这里先介绍下LayoutInflater类及其中的inflate方法的使用。
一、LayoutInflater类:

可以看到这是一个抽象类,他的作用是把layout的xml布局文件生成对应的View对象,并实例化,这个类的作用类似于findViewById(), 不同点是:
* LayoutInflater是用来找layout下xml布局文件的,而且它会实例化
* findViewById()是找具体xml布局文件下的具体widget控件,比如:Button按钮
该类的实例化方法有三种:
1、该类的from方法,从给定的context中获得
[code]LayoutInflater inflater = LayoutInflater.from(context);
Viewview = inflater.inflate(R.layout.mian, null);[/code]
2、通过SystemService获得,api里也是这么举例的
[code] LayoutInflater inflater = (LayoutInflater)context.getSystemServices(Context.LAYOUT_INFLATER_SERVICES);
View view = inflater.inflate(R.layout.main, null);[/code]
3、通过View类下子类window的一个函数getLayoutInflater()
[code]View layout = inflater.inflate(R.layout.main, null);[/code]
其实,这三个方法本质一样,在底层都是是用到了getSystemServices,
Activity的getLayoutInflater()方法是调用PhoneWindow的getLayoutInflater()方法,(刚才花了一个小时时间找PhoneWindow类,api里没有,感谢网友吧)看一下该源代码:
[code]public PhoneWindow(Contextcontext) {
super(context);
mLayoutInflater= LayoutInflater.from(context);
}[/code]
可以看出它其实是调用LayoutInflater.from(context)。
在来看下LayoutInflater.from(context)源码:
[code]public static LayoutInflater from(Context context) {
LayoutInflater LayoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if(LayoutInflater== null){
throw new AssertionError("LayoutInflaternot found.");
}
return LayoutInflater;
}[/code]
可以看出它其实调用context.getSystemService()。
ListView分批加载用到的就是第三个实例化方法,在activity里实例化页脚,然后把页脚上“正在加载”动画加到ListView上。
二、inflate方法:

可以看到该方法有四种重载形式,这里用的第一个方法,第一个参数resource是想要用的布局文件的id,第二个参数root是持有选项卡的内容,获取FrameLayout ,其他方法类似。
下面是网上的:
inflate就相当于将一个xml中定义的布局找出来.
* 因为如果在一个Activity文件里直接用findViewById()这个方法的话,
* 那么它所对应的是setConentView()中调用的那个layout里的组件.
* 因此如果在同样的Activity里用到别的layout的话,
* 而且你还要设置这个layout里的组件(比如:ImageView,TextView)上的内容,
* 那么你就必须用inflate()先将这个layout找出来, 然后再用这个layout对象去找到它上面的组件
* 然后进行一系列的操作
一句话,这个inflate就是个中介,你这个activity要用别的布局里的组件,你就要先找到这个布局,inflate就是这个角色了。
好了,就是这个样子。enjoy!!!
我来回答
回答3个
时间排序
认可量排序
认可0
认可0
认可0
或将文件直接拖到这里
悬赏:
E币
网盘
* 网盘链接:
* 提取码:
悬赏:
E币
Markdown 语法
- 加粗**内容**
- 斜体*内容*
- 删除线~~内容~~
- 引用> 引用内容
- 代码`代码`
- 代码块```编程语言↵代码```
- 链接[链接标题](url)
- 无序列表- 内容
- 有序列表1. 内容
- 缩进内容
- 图片
相关问答
-
2015-08-04 20:13:23
-
2015-08-05 18:29:58
-
2015-08-19 21:45:26
-
2020-11-11 10:09:54
-
2015-08-09 20:39:38
-
2015-12-09 13:18:28
-
2020-09-19 13:43:42
-
42015-10-14 18:49:25
-
2020-04-17 16:24:01
-
32015-08-17 20:38:04
-
2015-09-14 16:29:23
-
2018-12-21 09:57:13
-
2015-08-08 20:31:20
-
2024-09-03 14:25:04
-
2015-08-07 21:22:46
-
12015-08-17 21:09:26
-
2020-12-04 15:25:40
-
2015-08-31 10:02:50
-
2021-01-20 15:50:47
无更多相似问答 去提问

点击登录
-- 积分
-- E币
提问
—
收益
—
被采纳
—
我要提问
切换马甲
上一页
下一页
悬赏问答
-
53519dv500接lvds的sensor mn34120,图像出现很多竖线,sensor板接以前的3519v101没问题
-
103403外接hdmi口1024*600显示屏报错
-
5SS928点DC camera的6946,全屏紫色
-
5hi3519 的 网络传输的MTU值可以修改到比1500大嘛?
-
10WS73V100星闪扫描不到设备sle
-
5SS928/SD3403 录像失败 venc stream time out, exit thread; venc 2 stream buffer is full
-
10Hi3516DV500无法运行PQTool软件
-
10君正T23+1084带TF卡插卡(给该主板适配TF卡驱动,电机驱动,适配GPIO)
-
5Hi3536A从vpss获取1080分辨率的yuv数据异常
-
10谁有RV1126 最新版本SDK卖吗?
举报反馈
举报类型
- 内容涉黄/赌/毒
- 内容侵权/抄袭
- 政治相关
- 涉嫌广告
- 侮辱谩骂
- 其他
详细说明
提醒
你的问题还没有最佳答案,是否结题,结题后将扣除20%的悬赏金
取消
确认
提醒
你的问题还没有最佳答案,是否结题,结题后将根据回答情况扣除相应悬赏金(1回答=1E币)
取消
确认