Inflater英文意思是膨胀,在Android中应该是扩展的意思吧。
LayoutInflater的作用类似于 findViewById(),不同点是LayoutInflater是用来找layout文件夹下的xml布局文件,并且实例化!而 findViewById()是找具体某一个xml下的具体 widget控件(如:Button,TextView等)。
她可以有很多地方可以使用,如BaseAdapter的getView中,自定义Dialog中取得view中的组件widget等等。
它的用法有2种:
Java代码
-
view plaincopy to clipboardprint?
-
LayoutInflater inflater = LayoutInflater.from(this);
-
View view=inflater.inflate(R.layout.ID, null);
-
或者干脆并成一句:
- View view=LayoutInflater.from(this).inflate(R.layout.ID, null);
另一种方法:
Java代码
-
view plaincopy to clipboardprint?
-
LayoutInflater inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
- View view=inflater.inflate(R.layout.ID, null);
上面2种方法本质上是一样的,看下面的源码,form()调用的就是getSystemService():
Java代码
-
Java代码
-
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;
- }
另外getSystemService()是Android很重要的一个API,它是Activity的一个方法,根据传入的NAME来取得对应的Object,然后转换成相应的服务对象。以下介绍系统相应的服务。
传入的Name |
返回的对象 |
说明 |
WINDOW_SERVICE |
WindowManager |
管理打开的窗口程序 |
LAYOUT_INFLATER_SERVICE |
LayoutInflater |
取得xml里定义的view |
ACTIVITY_SERVICE |
ActivityManager |
管理应用程序的系统状态 |
POWER_SERVICE |
PowerManger |
电源的服务 |
ALARM_SERVICE |
AlarmManager |
闹钟的服务 |
NOTIFICATION_SERVICE |
NotificationManager |
状态栏的服务 |
KEYGUARD_SERVICE |
KeyguardManager |
键盘锁的服务 |
LOCATION_SERVICE |
LocationManager |
位置的服务,如GPS |
SEARCH_SERVICE |
SearchManager |
搜索的服务 |
VEBRATOR_SERVICE |
Vebrator |
手机震动的服务 |
CONNECTIVITY_SERVICE |
Connectivity |
网络连接的服务 |
WIFI_SERVICE |
WifiManager |
Wi-Fi服务 |
TELEPHONY_SERVICE |
TeleponyManager |
电话服务 |
分享到:
相关推荐
Android 中LayoutInflater的使用
android中LayoutInflater的使用.pdf
Android 中LayoutInflater(布局加载器)之实战篇 博客的Demo 博客地址: http://blog.csdn.net/l540675759/article/details/78112989 两种方式实现小红书的引导页: (1)自定义View (2)自定义LayoutInflater....
测试:Android 中LayoutInflater的使用 注意:Aj_04是用了调用另外一个界面,要注意调用的方法, 还一定还要在AndroidManifest.xml 中加上呢句:<activity android:name="LayoutInflaterDemo"></activity>
主要介绍了Android LayoutInflater分析的相关资料,需要的朋友可以参考下
NULL 博文链接:https://yufeng-zhu.iteye.com/blog/1676207
down-test-Android 获得 LayoutInflater 实例的三种方式
LayoutInflater Layout 与 Inflater[扩充]两个单词的合并,可以理解为:当前Activity 布局的扩充.
主要介绍了Android 中LayoutInflater.inflate()方法的介绍的相关资料,希望通过本文大家能掌握这部分内容,需要的朋友可以参考下
android inflate demo,方便新手使用,下载导入android studio就可直接运行,简单明了的展现inflate不同参数的区别
LayoutInflater类在我们日常开发中经常会用到,最近在使用中就遇到了一些问题,所有下面这篇文章主要给大家总结了关于Android中使用LayoutInflater要注意的一些坑,希望通过这篇能让大家避免走一些弯路,需要的朋友...
通过LayoutInflater.Factory实现主题切换功能,详见:http://blog.csdn.net/llew2011
主要给大家介绍了关于Android中LayoutInflater.inflater()的正确打开方式,文中通过示例代码介绍的非常详细,需要的朋友可以参考借鉴,下面随着小编来一起学习学习吧
主要介绍了Android开发之获取LayoutInflater对象的方法,结合实例形式总结分析了Android获取LayoutInflater对象的常用技巧,需要的朋友可以参考下
Android LayoutInflater加载布局详解 对于有一定Android开发经验的同学来说,一定使用过LayoutInflater.inflater()来加载布局文件,但并不一定去深究过它的原理,比如 1.LayoutInflater为什么可以加载layout文件? ...
在android中,LayoutInflater有点类似于Activity的findViewById(id),不同的是LayoutInflater是用来找layout下的xml布局文件,并且实例化!而findViewById()是找具体xml下的具体 widget控件(如:Button,TextView等)。...