Android LayoutInflater详解

在实际开发中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 inflater = LayoutInflater.from(context);  
3. LayoutInflater inflater =  (LayoutInflater)context.getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
其实,这三种方式本质是相同的,从源码中可以看出:
 1 getLayoutInflater():
 2 Activity 的 getLayoutInflater() 方法是调用 PhoneWindow 的getLayoutInflater()方法,看一下该源代码:
 3 public PhoneWindow(Context context)
 4 {
 5  super(context);
 6     mLayoutInflater = LayoutInflater.from(context);
 7 }
 8 可以看出它其实是调用 LayoutInflater.from(context)。
 9 LayoutInflater.from(context):
10 public static LayoutInflater from(Context context)
11 {
12  LayoutInflater LayoutInflater = (LayoutInflater) context.getSystemService
13          (Context.LAYOUT_INFLATER_SERVICE);
14     if (LayoutInflater == null)
15     {
16      throw new AssertionError("LayoutInflater not found.");
17     }
18     return LayoutInflater;
19 }

可以看出它其实调用 context.getSystemService()。
结论:所以这三种方式最终本质是都是调用的Context.getSystemService()。
另外getSystemService()是Android很重要的一个API,它是Activity的一个方法,根据传入的NAME来取得对应的Object,然后转换成相应的服务对象。以下介绍系统相应的服务。
传入的Name 返回的对象 说明
WINDOW_SERVICEWindowManager 管理打开的窗口程序
LAYOUT_INFLATER_SERVICELayoutInflater 取得xml里定义的view
ACTIVITY_SERVICEActivityManager 管理应用程序的系统状态
POWER_SERVICEPowerManger 电源的服务
ALARM_SERVICEAlarmManager 闹钟的服务
NOTIFICATION_SERVICENotificationManager 状态栏的服务
KEYGUARD_SERVICEKeyguardManager 键盘锁的服务
LOCATION_SERVICELocationManager 位置的服务,如GPS
SEARCH_SERVICESearchManager 搜索的服务
VEBRATOR_SERVICEVebrator 手机震动的服务
CONNECTIVITY_SERVICEConnectivity 网络连接的服务
WIFI_SERVICEWifiManager Wi-Fi服务
TELEPHONY_SERVICETeleponyManager 电话服务
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 等)。

转载于:https://www.cnblogs.com/wangying222/p/5337924.html

Android LayoutInflater详解相关推荐

  1. Android LayoutInflater详解(转)

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

  2. android service layoutinflater,[转]Android LayoutInflater详解

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

  3. Android SharedFlow详解

    转载请标明出处:http://blog.csdn.net/zhaoyanjun6/article/details/121911675 本文出自[赵彦军的博客] 文章目录 系列文章 什么是SharedF ...

  4. android fragment 优势,Android Fragment详解

    参考网址:Android Fragment详解 一.什么是Fragment? Fragment:是Android3.0开始新增的概念,意为碎片.Fragment是依赖于Activity的,不能独立存在 ...

  5. 【转】Android菜单详解——理解android中的Menu--不错

    原文网址:http://www.cnblogs.com/qingblog/archive/2012/06/08/2541709.html 前言 今天看了pro android 3中menu这一章,对A ...

  6. Android菜单详解——理解android中的Menu

    前言 今天看了pro android 3中menu这一章,对Android的整个menu体系有了进一步的了解,故整理下笔记与大家分享. PS:强烈推荐<Pro Android 3>,是我至 ...

  7. android Fragments详解

    android Fragments详解一:概述 android Fragments详解二:创建Fragment 转载于:https://my.oschina.net/liangzhenghui/blo ...

  8. android WebView详解,常见漏洞详解和安全源码(下)

    上篇博客主要分析了 WebView 的详细使用,这篇来分析 WebView 的常见漏洞和使用的坑.  上篇:android WebView详解,常见漏洞详解和安全源码(上)  转载请注明出处:http ...

  9. android WebView详解,常见漏洞详解和安全源码(上)

    这篇博客主要来介绍 WebView 的相关使用方法,常见的几个漏洞,开发中可能遇到的坑和最后解决相应漏洞的源码,以及针对该源码的解析.  由于博客内容长度,这次将分为上下两篇,上篇详解 WebView ...

最新文章

  1. 双指针找链表中点多种写法
  2. 使用Core Animation对象来实现动画
  3. C++ 对引用的理解2
  4. python怎么做软件界面_python – 如何自定义桌面应用程序的标题栏和窗口
  5. 手机支持html5绘图性能,【高级系列】Canvas绘制性能专题
  6. 随想录(学习使用virtualbox软件)
  7. (Deep learning)深度卷积网络实战——第一部分
  8. css常见样式命名思想
  9. Labview编程模式
  10. 约分最简分式java
  11. Python青少年学编程之秦九韶算法(初三、高中信息技术)
  12. 还在为JS闭包烦恼? FF带你一篇文章玩转闭包,某化腾听了都说好!!
  13. 微信也能设置主题了,盘他!
  14. 手机计算机表白公式,表白公式数学公式
  15. 安防无战事:一场10213亿元的误会
  16. python关于FIFA球员的数据分析
  17. 马云唱京剧《空城计》,柳传志说相声:“商界春晚”大佬们真会玩(附视频)...
  18. java.lang.IllegalArgumentException: bound must be positive
  19. leetcode 刷题ing
  20. 医学图像分割新网络:Boundary-aware Context Neural Network for Medical Image Segmentation

热门文章

  1. 图像处理之基于NCC模板匹配识别
  2. 多集群应用如何帮助企业级Kubernetes获益
  3. Java_异常_05_ OutOfMemoryError: Java heap space
  4. restful-api最佳实践
  5. ExtJS学习笔记3:载入、提交和验证表单
  6. Follow me!百万奖金由你拿 | 精准资助机器学习(三)
  7. 开启apache的server-status辅助分析工具
  8. 在ASP.NET中重写URL 方法三:在IIS7中使用HttpModule 实现无扩展名的URL重写
  9. 算法62---最长回文子序列长度(子串)、回文子序列总共个数(子串)【动态规划】...
  10. PostgreSQL的那点事儿