http://dev.10086.cn/cmdn/wiki/index.php?doc-view-6066.html

Inflater英文意思是膨胀,在Android中应该是扩展的意思吧。
LayoutInflater的作用类似于 findViewById(),不同点是LayoutInflater是用来找layout文件夹下的xml布局文件,并且实例化!而 findViewById()是找具体某一个xml下的具体 widget控件(如:Button,TextView等)。

(0)她可以有很多地方可以使用,如BaseAdapter的getView中,自定义Dialog中取得view中的组件widget等等。
它的用法有2种:

复制到剪贴板Java代码
  1. viewplaincopytoclipboardprint?
  2. LayoutInflaterinflater=LayoutInflater.from(this);
  3. Viewview=inflater.inflate(R.layout.ID,null);
  4. 或者干脆并成一句:
  5. Viewview=LayoutInflater.from(this).inflate(R.layout.ID,null);


另一种方法: 复制到剪贴板Java代码
  1. viewplaincopytoclipboardprint?
  2. LayoutInflaterinflater=(LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
  3. Viewview=inflater.inflate(R.layout.ID,null);


上面2种方法本质上是一样的,看下面的源码,form()调用的就是getSystemService(): 复制到剪贴板Java代码
  1. Java代码
  2. publicstaticLayoutInflaterfrom(Contextcontext){
  3. LayoutInflaterLayoutInflater=
  4. (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  5. if(LayoutInflater==null){
  6. thrownewAssertionError("LayoutInflaternotfound.");
  7. }
  8. returnLayoutInflater;
  9. }



另外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 电话服务


复制到剪贴板Java代码
  1. Java代码
  2. //基本用法
  3. publicvoidshowCustomDialog(){
  4. AlertDialog.Builderbuilder;
  5. AlertDialogalertDialog;
  6. ContextmContext=AppActivity.this;
  7. //下面俩种方法都可以
  8. //LayoutInflaterinflater=getLayoutInflater();
  9. LayoutInflaterinflater=(LayoutInflater)
  10. mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
  11. Viewlayout=inflater.inflate(R.layout.custom_dialog,null);
  12. TextViewtext=(TextView)layout.findViewById(R.id.text);
  13. text.setText("Hello,WelcometoMrWei'sblog!");
  14. ImageViewimage=(ImageView)layout.findViewById(R.id.image);
  15. image.setImageResource(R.drawable.icon);
  16. builder=newAlertDialog.Builder(mContext);
  17. builder.setView(layout);
  18. alertDialog=builder.create();
  19. alertDialog.show();
  20. }
  21. }
  22. protectedvoidshowToast(inttype){
  23. Toast.makeText(this,"*********",Toast.LENGTH_LONG).show();
  24. LayoutInflaterli=(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  25. Viewview=li.inflate(R.layout.toast,null);
  26. Toasttoast=newToast(this);
  27. toast.setView(view);
  28. toast.setDuration(type);
  29. toast.show();
  30. }


更多相关文章

  1. Android 记忆卡片游戏 记忆力 Android游戏 Android记忆卡片游戏
  2. android 源代码在线查看和索引
  3. Android系统默认Home应用程序(Launcher)的启动过程源代码分析(3)
  4. Android通过chrome插件在线查看Android源代码
  5. 关于repo下载android代码时候错误的处理。
  6. Android系统在新进程中启动自定义服务过程(startService)的原理分
  7. Android 之如何添加 android private libraries 中的包的源代码
  8. Android 7.0 之后抓包 unknown 和证书无效的解决方案(无需改代码)

随机推荐

  1. Android获取屏幕尺寸
  2. Android(安卓)C++ 动态加载so
  3. Android(安卓)以友好的方式显示时间
  4. Android触摸屏手势识别
  5. android 8.0 設置Ethernet的靜態IP
  6. Android(安卓)Audio代码分析23 - attachA
  7. android 邮件地址正则表达式
  8. Android(安卓)创建桌面快捷键
  9. LinearLayout(线性布局)
  10. Android中http交互是用HttpUrlConnection