一.作用:

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

   二.获得 LayoutInflater 实例的三种方式

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

          2.LayoutInflater inflater = LayoutInflater.from(this);       

          3.LayoutInflater inflater = (LayoutInflater)Context.getSystemService(LAYOUT_INFLATER_SERVICE);

          

          其实,这三种方式本质是相同的,从源码中可以看出:

          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()。

 

      结论:所以这三种方式最终本质是都是调用的Context.getSystemService()。

三.实例化LayoutInflater之后,就要将layout的xml布局文件实例化为View类对象。

    1.

LayoutInflater inflater = getLayoutInflater(); 
View view=inflater.inflate(R.layout.ID, null);    

  2.

LayoutInflater inflater = LayoutInflater.from(this);  
View view=inflater.inflate(R.layout.ID, null);

3.

LayoutInflater inflater = (LayoutInflater)Context.getSystemService(LAYOUT_INFLATER_SERVICE);
View view=inflater.inflate(R.layout.ID, null);

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

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

           

            示意代码:

1 LayoutInflater inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);  
2   
3 View view = inflater.inflate(R.layout.custom, (ViewGroup)findViewById(R.id.test));  
4   
5 //EditText editText = (EditText)findViewById(R.id.content);// error  
6 EditText editText = (EditText)view.findViewById(R.id.content);  

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

 

   因为时间关系,demo楼主也没写,这个是我在网上转载的一个小demo,大家可以看看!

(以下为转载)

   为了让大家容易理解我[转]做了一个简单的Demo,主布局main.xml里有一个TextView和一个Button,当点击Button,出现 Dialog,而这个Dialog的布局方式是我们在layout目录下定义的custom_dialog.xml文件(里面左右分布,左边 ImageView,右边TextView)。

[转载]LayoutInflater作用及使用
 1 package com.bivin;
 2 
 3 import android.app.Activity;
 4 import android.app.AlertDialog;
 5 import android.content.Context;
 6 import android.os.Bundle;
 7 import android.view.LayoutInflater;
 8 import android.view.View;
 9 import android.view.View.OnClickListener;
10 import android.widget.Button;
11 import android.widget.ImageView;
12 import android.widget.TextView;
13 
14 public class MainActivity extends Activity implements OnClickListener {
15 
16 private Button button;
17 
18 public void onCreate(Bundle savedInstanceState) {
19 super.onCreate(savedInstanceState);
20 setContentView(R.layout.main);
21 
22 button = (Button) findViewById(R.id.button);
23 button.setOnClickListener(this);
24 }
25 
26 @Override
27 public void onClick(View v) {
28 
29 showCustomDialog();
30 }
31 
32 public void showCustomDialog() {
33 AlertDialog.Builder builder;
34 AlertDialog alertDialog;
35 Context mContext = MainActivity.this;
36 
37 LayoutInflater inflater = (LayoutInflater) mContext
38 .getSystemService(LAYOUT_INFLATER_SERVICE);
39 View layout = inflater.inflate(R.layout.custom_dialog, null);
40 TextView text = (TextView) layout.findViewById(R.id.text);
41 text.setText("Hello, Welcome to Mr Wei's blog!");
42 ImageView image = (ImageView) layout.findViewById(R.id.image);
43 image.setImageResource(R.drawable.icon);
44 builder = new AlertDialog.Builder(mContext);
45 builder.setView(layout);
46 alertDialog = builder.create();
47 alertDialog.show();
48 }
49 }

 

   

 

posted on 2013-01-08 14:35  merryjd  阅读(5219)  评论(0编辑  收藏  举报