LayoutInflate

LayoutInflater 在 android 开发中使用频率较高,今天谈谈!
该类是一个抽象类,在文档中如下声明:

view plain copy to clipboard print ?

  1. public abstract class LayoutInflater extends Object  


1.  获得 LayoutInflater 实例

三种方法可以获得该实例对象,方法如下:

view plain copy to clipboard print ?

  1. a. LayoutInflater inflater = getLayoutInflater();  
  2.   
  3.   
  4. b. LayoutInflater localinflater =  
  5.         (LayoutInflater)context.getSystemService  
  6.             (Context.LAYOUT_INFLATER_SERVICE);   
  7.   
  8.   
  9. c. LayoutInflater inflater = LayoutInflater.from(context);  

对于方法 a,主要是调用 Activity 的 getLayoutInflater() 方法。继续跟踪研究 android 源码,Activity 中的该方法是调用 PhoneWindow 的

getLayoutInflater()方法,那么,分享一下该源代码:

view plain copy to clipboard print ?

  1. public PhoneWindow(Context context) {  
  2.         super(context);  
  3.         mLayoutInflater = LayoutInflater.from(context);  
  4. }<span style="font-family: Arial, Verdana, sans-serif; white-space: normal; "> </span>  

可以看出它其实是调用 LayoutInflater.from(context),那么该方法其实是调用 b,看看源码,如下:

view plain copy to clipboard print ?

  1. /** 
  2.     * Obtains the LayoutInflater from the given context. 
  3.     */  
  4.    public static LayoutInflater from(Context context) {  
  5.        LayoutInflater LayoutInflater =  
  6.                (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
  7.        if (LayoutInflater == null) {  
  8.            throw new AssertionError("LayoutInflater not found.");  
  9.        }  
  10.        return LayoutInflater;  
  11.    }  


2. inflate 方法

inflate 愿意是充气之类的,在这里主要意思就是,扩张、使之膨胀。换句话说就是将当前视图view 补充完整、扩展该视图。

通过 sdk 的 api 文档,可以知道该方法有以下几种过载形式,返回值均是 View 对象,如下:

view plain copy to clipboard print ?

  1. public View inflate (int resource, ViewGroup root)  
  2.   
  3.   
  4. public View inflate (XmlPullParser parser, ViewGroup root)  
  5.   
  6.   
  7. public View inflate (XmlPullParser parser, ViewGroup root, boolean attachToRoot)  
  8.   
  9.   
  10. public View inflate (int resource, ViewGroup root, boolean attachToRoot)  

示意代码:

view plain copy to clipboard print ?

  1. LayoutInflater inflater = (LayoutInflater)  
  2.     getSystemService(LAYOUT_INFLATER_SERVICE);  
  3.   
  4.   
  5. /* R.id.test 是 custom.xml 中根(root)布局 LinearLayout 的 id */  
  6. View view = inflater.inflate(R.layout.custom,  
  7.          (ViewGroup)findViewById(R.id.test));  
  8. /* 通过该 view 实例化 EditText对象, 否则报错,因为当前视图不是custom.xml.即没有 setContentView(R.layout.custom) 或者 addView() */  
  9. //EditText editText = (EditText)findViewById(R.id.content);// error   
  10. EditText editText = (EditText)view.findViewById(R.id.content);  

对于上面代码,指定了第二个参数 ViewGroup root,当然你也可以设置为 null 值。

注意:该方法与 findViewById 方法不同。inflater 是用来找 layout 下 xml 布局文件,并且实例化!而 findViewById() 是找具体 xml 下的具体 widget 控件(如:Button,TextView 等)。

 

 LayoutInflater的作用类似于 findViewById(),不同点是LayoutInflater是用来找layout文件夹下的xml布局文件,并且实例化!而 findViewById()是找具体某一个xml下的具体 widget控件(如:Button,TextView等)。

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

Java代码
  1. LayoutInflater inflater = LayoutInflater.from(this);     
  2. View view=inflater.inflate(R.layout.ID, null);    
  3. 或者干脆并成一句:    
  4. View view=LayoutInflater.from(this).inflate(R.layout.ID, null);    

另一种方法: 

Java代码
  1. LayoutInflater inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);    
  2. 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.     }