android inflater 用法

在 实际开发中LayoutInflater这个类还是非常有用的,它的作用类似于findViewById()。不同点是LayoutInflater是用 来找res/layout/下的xml布局文件,并且实例化;而findViewById()是找xml布局文件下的具体widget控件(如 Button、TextView等)。 具体作用: 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 方法

inflate()方法一般接收两个参数,第一个参数就是要加载的布局id,第二个参数是指给该布局的外部再嵌套一层父布局,如果不需要就直接传null。这样就成功成功创建了一个布局的实例,之后再将它添加到指定的位置就可以显示出来了。
通过 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 等)。

##############

http://blog.sina.com.cn/s/blog_48a45b950100u8ax.html

inflater用法相关推荐

  1. Android inflater用法

    Android inflater用法 简介 具体作用 获取layoutInflater的三种方式 源码查看 返回值结果 注意 简介 在实际的开发中layoutInflater这个类的主要的主要的作用类 ...

  2. android之inflater用法

    在实际开发中LayoutInflater这个类还是非常有用的,它的作用类似于findViewById().不同点是LayoutInflater是用来找res/layout/下的xml布局文件,并且实例 ...

  3. java inflater_android inflater 用法

    在实际开发中LayoutInflater这个类还是非常有用的,它的作用类似于findViewById().不同点是LayoutInflater是用来找res/layout/下的xml布局文件,并且实例 ...

  4. android之inflater介绍

    image.png LayoutInflater(布局加载器) 开发中LayoutInflater这个类还是非常有用的,它的作用类似于findViewById(). 不同点是LayoutInflate ...

  5. Deflater 和 Inflater 的用法

    资料 Deflater 和 Inflater 的用法 String inputString = "blahblahblah??";byte[] input = inputStrin ...

  6. java inflater_Java Inflater finished()用法及代码示例

    如果已到达压缩数据流的末尾,则Inflater类的finished()函数将返回true. 函数签名: public boolean finished() 用法: i.finished(); 参数:该 ...

  7. inflater的用法

    在实际开发中LayoutInflater这个类还是非常有用的,它的作用类似于findViewById().不同点是LayoutInflater是用来找res/layout/下的xml布局文件,并且实例 ...

  8. android switcher控件,Android ViewSwitcher 的功能与用法

    ViewSwitcher 代表了视图切换组件, 本身继承了FrameLayout ,可以将多个View叠在一起 ,每次只显示一个组件.当程序控制从一个View切换到另个View时,ViewSwitch ...

  9. LayoutInflater的inflate函数用法详解

    LayoutInflater的inflate函数用法详解 LayoutInflater作用是将layout的xml布局文件实例化为View类对象. 获取LayoutInflater的方法有如下三种: ...

最新文章

  1. Science Bulletin:崔杰组发表了深浅海软甲纲动物比较病毒组学分析成果
  2. python真的超过java了吗-JAVA会被Python超越成为世界上第一大编程语言吗?
  3. linux设置静态IP及克隆后修改IP和hostname
  4. 矩阵快速幂及斐波那契数列模板
  5. 程序员的思考--终于确定了自己的技术发展方向
  6. php遍历文件夹下文件内容_php实现的遍历文件夹下所有文件,编辑删除
  7. LabVIEW 使用入门(简明笔记)
  8. Python自动化办公:xlwt万字教程
  9. 如何区分电压串联负反馈电路和电流串联负反馈电路
  10. 论文阅读笔记:Masked Autoencoders Are Scalable Vision Learners
  11. 推荐算法之AFM模型(注意力机制的引入)
  12. 刀具、砂轮的过程监视和控制系统
  13. onCreate()方法
  14. 2017年手机html5的发展,HTML5明年年中发展成熟 或带来行业洗牌
  15. HTML预格式化文本pre标签
  16. 袁国宝:从内容营销到超级传播,数字时代如何快速引爆产品和品牌
  17. 安卓基础控件(EditText、Button)以及布局(约束布局)的介绍
  18. 搬运! Windbg调试命令详解
  19. 银监会计算机类真题2019,2019国考之中国银监会招聘计算机类如何备考?
  20. 整数的左移运算符和右移运算符

热门文章

  1. App业务流量抓包方法
  2. 夏天电动汽车起火事故频繁,在于它实在太怕热了
  3. redis 数据类型与使用场景
  4. js charcodeat java_[js] charAt()、charCodeAt()、fromCharCode()
  5. 程序猿 常规SQL面试题
  6. 【unity基础】点乘和叉乘
  7. python 网络文件传输(邮件+百度网盘(百度云盘)+阿里云盘)
  8. 数据中心内负载均衡-ECMP的使用分析
  9. android 讯飞语音 提示组件未安装,讯飞语音听写API 未安装组件 错误码21001
  10. Mac用终端连接mysql数据库(终极版)