`

android Inflater

阅读更多

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

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

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



另一种方法: 

复制到剪贴板   Java代码
  1. view plaincopy to clipboardprint?  
  2. LayoutInflater inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);    
  3. View view=inflater.inflate(R.layout.ID, null );    


上面2种方法本质上是一样的,看下面的源码,form()调用的就是getSystemService(): 

 

复制到剪贴板   Java代码
  1. Java代码  
  2. public   static  LayoutInflater from(Context context) {       
  3.     LayoutInflater LayoutInflater =       
  4.             (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);       
  5.     if  (LayoutInflater ==  null ) {       
  6.         throw   new  AssertionError( "LayoutInflater not found." );       
  7.     }       
  8.     return  LayoutInflater;       
  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. public   void  showCustomDialog(){    
  4.   AlertDialog.Builder builder;    
  5.   AlertDialog alertDialog;    
  6.   Context mContext = AppActivity.this ;    
  7. //下面俩种方法都可以     
  8.   //LayoutInflater inflater = getLayoutInflater();     
  9.   LayoutInflater inflater = (LayoutInflater)     
  10. mContext.getSystemService(LAYOUT_INFLATER_SERVICE);    
  11.   View layout = inflater.inflate(R.layout.custom_dialog,null );    
  12.   TextView text = (TextView) layout.findViewById(R.id.text);    
  13.   text.setText("Hello, Welcome to Mr Wei's blog!" );    
  14.   ImageView image = (ImageView) layout.findViewById(R.id.image);    
  15.   image.setImageResource(R.drawable.icon);    
  16.   builder = new  AlertDialog.Builder(mContext);    
  17.   builder.setView(layout);    
  18.   alertDialog = builder.create();    
  19.   alertDialog.show();    
  20.  }    
  21. }    
  22.     
  23. protected   void  showToast( int  type) {      
  24.         Toast.makeText(this "*********" , Toast.LENGTH_LONG).show();      
  25.       
  26.         LayoutInflater li = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);      
  27.         View view = li.inflate(R.layout.toast, null );      
  28.               
  29.         Toast toast = new  Toast( this );      
  30.         toast.setView(view);      
  31.         toast.setDuration(type);      
  32.         toast.show();      
  33.     }     
分享到:
评论

相关推荐

    Android LayoutInflater中 Inflate()方法应用

    `inflate()` 方法是`LayoutInflater` 的核心方法,它的作用是解析XML布局文件,并将其转换为Android视图层次结构。下面将详细阐述`inflate()` 方法的用法及其与其他方法的区别。 首先,`inflate()` 方法的基本使用...

    Android LayoutInflater.inflate()详解及分析

    Android LayoutInflater.inflate()详解 深入理解LayoutInflater.inflate() 由于我们很容易习惯公式化的预置代码,有时我们会忽略很优雅的细节。LayoutInflater以及它在Fragment的onCreateView()中填充View的方式...

    inflate的使用

    在Android开发中,`inflate`是一个非常关键的操作,主要用于将XML布局文件转换为视图对象并添加到父视图中。这个过程被称为“布局解析”或“视图创建”。`inflate`方法通常在Activity、Fragment或者自定义ViewGroup...

    Android 中LayoutInflater.inflate()方法的介绍

    Android 中LayoutInflater.inflate()方法的介绍 最近一直想弄明白LayoutInflater对象的inflate方法的用法,今天做了实例。 <LinearLayout android:id=@+id/ll_item_Group android:layout_width=match_parent ...

    LayoutInflater inflate例子

    总结一下,`LayoutInflater` 的`inflate()` 方法是Android开发中实现动态布局和视图复用的关键工具。理解其工作原理和用法,可以提高代码的灵活性和效率,使开发者能更好地控制应用程序的界面呈现。通过合理利用`...

    LayoutInflater inflate 示例demo

    通过下载并导入到Android Studio,你可以直接运行此项目,直观地看到`inflate()`的各种使用场景。 首先,`LayoutInflater`通常从资源ID获取,可以使用`getSystemService()`方法和`Context.LAYOUT_INFLATER_SERVICE`...

    LayoutInflater.from(context).inflate()方法的调研

    在Android开发中,`LayoutInflater.from(context).inflate()`方法是一个至关重要的组件,用于将XML布局文件转换为视图对象并添加到视图层次结构中。这个方法广泛应用于动态加载和构建用户界面,尤其在处理列表视图、...

    博客《ListView滑动删除实现之一——merge标签与LayoutInflater.inflate()》对应源码

    博客《ListView滑动删除实现之一——merge标签与LayoutInflater.inflate()》对应源码,博客地址:http://blog.csdn.net/harvic880925/article/details/45155965

    Android LayoutInflater.inflate源码分析

    总结来说,`LayoutInflater.inflate()` 方法是Android中解析XML布局并创建视图的核心工具。理解它的源码和不同参数的含义,有助于优化性能和提高代码的可维护性。在实际开发中,选择合适的 `inflate()` 方法版本,能...

    Android getViewById和getLayoutInflater().inflate()的详解及比较

    在Android开发中,`findViewById()` 和 `getLayoutInflater().inflate()` 是两个非常重要的方法,它们各自服务于不同的目的。本文将深入探讨这两个方法的功能、用法以及它们之间的差异。 首先,我们来了解一下 `...

    Android代码-Android-FloatWindow

    View contentView = inflater.inflate(R.layout.layout_pop, null); NewFloatMainWindow.getFloatMainWindow(MainActivity.this, NewFloatMainWindow.LOCATION_LEFT, contentView); 3.实现悬浮窗如此简单 readme-...

    Android Tablayout 自定义Tab布局的使用案例

    Android Tablayout 自定义Tab布局的使用案例 Android Tablayout 是 Android 设计库中的一部分,主要用于实现标签页功能。Tablayout 中的 Tab 可以自定义布局,以满足不同的需求。本文将 introduction 了 Android ...

    Android开发中setContentView和inflate的区别分析

    本文实例讲述了Android开发中setContentView和inflate的区别。分享给大家供大家参考,具体如下: 一般用LayoutInflater做一件事:inflate inflate这个方法总共有四种形式(见下面),目的都是把xml表述的layout转化...

    Android 底部弹出dialog+动画

    View view = LayoutInflater.from(getContext()).inflate(R.layout.dialog_custom, null); // 初始化并设置Dialog属性 Dialog dialog = new AlertDialog.Builder(getContext()) .setView(view) .create(); ...

    android dialog输入框获取数据

    ### Android Dialog 输入框获取数据详解 #### 一、前言 在Android开发中,Dialog(对话框)是一种非常常见的UI组件,它可以帮助开发者快速构建出弹出式对话窗口,用于与用户进行简单交互,比如提示信息、确认操作...

    android menu菜单距中显示

    在Android应用开发中,`Menu`是用户界面中不可或缺的一部分,它通常用于在特定操作(如选项、更多操作)上为用户提供快捷访问的途径。在Android系统中,`Menu`通常出现在活动(Activity)的顶部作为选项菜单,或者在...

    Android自定义输入法软键盘

    View keyboardView = getLayoutInflater().inflate(R.layout.keyboard, null); // 初始化键盘上的按钮,设置点击事件等 return keyboardView; } ``` 布局文件(如`keyboard.xml`)通常包含一系列的`Button`或`...

Global site tag (gtag.js) - Google Analytics