当前位置: 首页 > news >正文

android inflater 用法

在实际开发中LayoutInflater这个类还是非常有用的(inflater是充气机的意思,这是是指填充),它的作用类似于findViewById()。不同点是LayoutInflater是用来找res/layout/下的xml布局文件,并且实例化;而findViewById()是找xml布局文件下的具体widget控件(如 Button、TextView等)(widget是小器具,小装置的意思,这里指控件)。 具体作用:

1、对于一个没有被载入或者想要动态载入的界面,都需要使用LayoutInflater.inflate()来载入;

2、对于一个已经载入的界面,就可以使用Activiyt.findViewById()方法来获得其中的界面元素。

LayoutInflater 是一个抽象类,在文档中如下声明:

public abstract class LayoutInflater extends Object

获得 LayoutInflater 实例的三种方式

1.LayoutInflater inflater = getLayoutInflater(); //调用Activity的getLayoutInflater()

2.LayoutInflater localinflater =(LayoutInflater)context.getSystemService (Context.LAYOUT_INFLATER_SERVICE);

3.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(Context context) {   
    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简介
  • 智能手机及操作系统
  • Android SDK是什么
  • windows命令行设置IP与DNS
  • 数据统计分析常用指标
  • Okhttp3基本使用
  • NPAPI 为什么会被 Chrome 禁用
  • DOM模型
  • 深入理解vue.js双向绑定的实现原理
  • MVC、MVP、MVVM模式的概念与区别
  • Js中的数据属性和访问器属性
  • es5和es6的区别
  • CSS3 选择器
  • 科普一下SM系列国密算法(从零开始学区块链)
  • JavaScript this 关键字
  • php的引用
  • [ 一起学React系列 -- 8 ] React中的文件上传
  • es6(二):字符串的扩展
  • golang 发送GET和POST示例
  • HTTP中的ETag在移动客户端的应用
  • JavaScript对象详解
  • Java知识点总结(JDBC-连接步骤及CRUD)
  • js继承的实现方法
  • js正则,这点儿就够用了
  • Nacos系列:Nacos的Java SDK使用
  • Transformer-XL: Unleashing the Potential of Attention Models
  • 程序员该如何有效的找工作?
  • 番外篇1:在Windows环境下安装JDK
  • 近期前端发展计划
  • 那些年我们用过的显示性能指标
  • ​sqlite3 --- SQLite 数据库 DB-API 2.0 接口模块​
  • ​软考-高级-信息系统项目管理师教程 第四版【第19章-配置与变更管理-思维导图】​
  • ​总结MySQL 的一些知识点:MySQL 选择数据库​
  • (003)SlickEdit Unity的补全
  • (02)Hive SQL编译成MapReduce任务的过程
  • (1)(1.11) SiK Radio v2(一)
  • (1)安装hadoop之虚拟机准备(配置IP与主机名)
  • (MATLAB)第五章-矩阵运算
  • (Redis使用系列) Springboot 实现Redis消息的订阅与分布 四
  • (附源码)springboot码头作业管理系统 毕业设计 341654
  • (算法)前K大的和
  • (转)用.Net的File控件上传文件的解决方案
  • (转)总结使用Unity 3D优化游戏运行性能的经验
  • (轉貼) VS2005 快捷键 (初級) (.NET) (Visual Studio)
  • *(长期更新)软考网络工程师学习笔记——Section 22 无线局域网
  • .a文件和.so文件
  • .net core Swagger 过滤部分Api
  • .NET Standard / dotnet-core / net472 —— .NET 究竟应该如何大小写?
  • .NET 中使用 TaskCompletionSource 作为线程同步互斥或异步操作的事件
  • .net连接oracle数据库
  • [ 渗透工具篇 ] 一篇文章让你掌握神奇的shuize -- 信息收集自动化工具
  • [3300万人的聊天室] 作为产品的上游公司该如何?
  • [APIO2012] 派遣 dispatching
  • [BZOJ] 1001: [BeiJing2006]狼抓兔子
  • [C/C++]数据结构 栈和队列()