【Android】inflater的作用
在实际开发中LayoutInflater这个类还是非常有用的,它的作用类似于findViewById()。不同点是LayoutInflater是用来找res/layout/下的xml布局文件,并且实例化;而findViewById()是找xml布局文件下的具体widget控件(如Button、TextView等)。
具体作用:
1、对于一个没有被载入或者想要动态载入的界面,都需要使LayoutInflater.inflate()来载入;
2、对于一个已经载入的界面,就可以使用Activiyt.findViewById()方法来获得其中的界面元素。
LayoutInflater 是一个抽象类,在文档中如下声明:
public abstract class LayoutInflaterextends Object
获得 LayoutInflater 实例的三种方式
- LayoutInflater inflater =getLayoutInflater(); //调用Activity的getLayoutInflater()
- LayoutInflater localinflater = (LayoutInflater)context.getSystemService (Context.LAYOUT_INFLATER_SERVICE);
- LayoutInflater inflater =LayoutInflater.from(context);
其实,这三种方式本质是相同的,从源码中可以看出:
- getLayoutInflater():
Activity 的 getLayoutInflater()方法是调用 PhoneWindow 的getLayoutInflater()方法,看一下该源代码:
public PhoneWindow(Context context) { super(context); mLayoutInflater = LayoutInflater.from(context); }
可以看出它其实是调用 LayoutInflater.from(context)。
- LayoutInflater.from(context):
public static LayoutInflater from(Contextcontext) { LayoutInflater LayoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); if (LayoutInflater == null) { throw new AssertionError("LayoutInflater not found."); } return LayoutInflater; }
可以看出它其实调用 context.getSystemService()。
结论:所以这三种方式最终本质是都是调用的Context.getSystemService()。
inflate 方法
通过 sdk 的 api 文档,可以知道该方法有以下几种过载形式,返回值均是 View 对象,如下:
public View inflate (int resource,ViewGroup root)
public View inflate (XmlPullParser parser,ViewGroup root)
public View inflate (XmlPullParser parser,ViewGroup root, boolean attachToRoot)
public View inflate (int resource,ViewGroup root, boolean attachToRoot)
示意代码:
LayoutInflater inflater =(LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE); View view =inflater.inflate(R.layout.custom, (ViewGroup)findViewById(R.id.test)); //EditText editText =(EditText)findViewById(R.id.content);// error EditText editText =(EditText)view.findViewById(R.id.content);
对于上面代码,指定了第二个参数 ViewGroup root,当然你也可以设置为 null 值。
注意:
== ·inflate 方法与 findViewById 方法不同;==
== ·inflater 是用来找 res/layout 下的 xml 布局文件,并且实例化;==
== ·findViewById() 是找具体 xml 布局文件中的具体 widget 控件(如:Button、TextView 等)。==
【Android】inflater的作用相关推荐
- Android inflater用法
Android inflater用法 简介 具体作用 获取layoutInflater的三种方式 源码查看 返回值结果 注意 简介 在实际的开发中layoutInflater这个类的主要的主要的作用类 ...
- android的适配器作用,适配器在Android中的作用是什么?
适配器在Android中的作用是什么? 我想知道在Android环境中何时,何地以及如何使用适配器. 来自Android开发者文档的信息对我来说不够,我希望得到更详细的分析. 11个解决方案 39 v ...
- 腾讯、网易云、字节跳动面试点总结—AMS在Android起到什么作用?
本专栏专注分享大型Bat面试知识,后续会持续更新,喜欢的话麻烦点击一个关注 面试官: AMS在Android起到什么作用,简单的分析下Android的源码 心理分析:这道题在发生在大多数场景下.面对这 ...
- android 自定义xmlns,Android xmlns 的作用及其自定义实例详解
Android xmlns 的作用及其自定义实例详解 xmlns:Android="http://schemas.android.com/apk/res/android的作用是: 这个是xm ...
- URI是什么,在Android中有什么作用?
URI是什么,在Android中有什么作用? Android中的URI即通用资源标识符,Universal Resource Identifier, 简称URI. Uri代表要操作的数据,Androi ...
- android inflater,探索 Android Inflater.inflate()
android-knowledge.jpg 在 Android 开发中,有很多看起来 "约定俗成" 的代码,有时候不妨停下来想一想深层次的含义.最近我在给 Fragment 填充布 ...
- Android Environment 的作用以及常用的方法
从源码中可以看到这样一句话 Provides access to environment variables Environment的作用是: 提供对环境变量的访问 常见的方法 //外部存储设备的当 ...
- android 焦点的作用,Android中的视图焦点Focus的详细介绍
在非触摸屏设备中接收事件和处理响应的控件是具有焦点(Focused)的控件.一个窗口中一个时间内只能有一个具有焦点的控件.在早期具有滚轮设备的android系统中以及现在的智能TV电视应用中视图的焦点 ...
- Android适配器以及作用,Android RecyclerView 通用适配器的实现
分析 我们需要在RecyclerViewAdapter内部维持一个数据集合的引用 需要实现Item的点击事件,包括短按和长按,并需要向外部提供设置监听器的方法 这个适配器必须通用,所以应该是泛型的,并 ...
最新文章
- 关于Git的几个使用技巧
- mac iTunes启动失败,声称iTunes文件夹被锁定
- 反编译工具Reflector下载(集成FileGenerator和FileDisassembler)
- NLog日志框架使用探究
- C#读写txt文件的两种方法介绍
- 这位教授2 年一篇 Science,再获教科书级的重大发现
- 在HTML中使用WCF RESTful上传文件
- 【Kubernetes】mac 安装minikube
- 检讨:丢了我女儿和项目设计感想
- php redis 日志模式,Redis的PHP客户端
- mysql的远程服务开启_mysql开启远程服务
- python 流写入文件_Python数据流写入文件
- 用ICC编译MP3编码器LAME
- 12月第1周网络安全报告:85.9万境内主机感染病毒
- 迪杰斯特拉模板-刘汝佳紫书
- C# 使用斑马打印机打印图片
- 冰冻三尺,非一日之寒。数据解析——xpath(2)
- 行尸走肉第一季/全集The Walking Dead迅雷下载
- 有限元剖网格之Gmsh安装与使用入门
- Cisco ❀ 数据封装
热门文章
- php搜索ttfb用时太长,waiting TTFB时间过长怎么办
- ITK系列28_ 区域增长(置信连接)对PNG向量图像进行二维分割
- [手搓人]大战[高亮编辑器/查找替换/函数跳跳蛙]--巅峰对决(2)
- UEA1224——郭恩赐 day03 作业
- 关于可编辑div contentEditable=true 添加表情光标设置问题
- 毕业设计-基于深度学习的花卉识别分类
- Nanopc 使用mumble实现实时语音通信功能
- ansible firewalld 模块
- Unity Mathf
- 做网络营销的心态与步骤