LayoutInflater 三种获得方式

LayoutInflater 作用是从外部加载一个xml布局文件。

 

获得 LayoutInflater 实例的三种方式:

1.LayoutInflater inflater = getLayoutInflater();  //调用Activity的getLayoutInflater()

2.LayoutInflater localinflater =(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

3. LayoutInflater inflater = LayoutInflater.from(context)

分析:

getLayoutInflater():

Activity 中 getLayoutInflater() 调用 PhoneWindow 的getLayoutInflater()方法

public PhoneWindow(Context context) {         

   super(context);         

   mLayoutInflater = LayoutInflater.from(context); 

 }

最后调用 LayoutInflater.from(context)。

 

LayoutInflater.from(context):

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;  

 }

其实最后者是调 context.getSystemService()。系统提供的解析服务。

 

posted on 2017-11-23 11:47  wp7ers  阅读(420)  评论(0)    收藏  举报