LayoutInflater的作用和findViewById()差不多,不同之处在于LayoutInflater是找layout文件夹下的某个xml布局文件!而 findViewById()是找某一个xml下的具体 widget控件。

LayoutInflater可以将一个xml布局文件转化为View 类型,其代码如下:

View Code
 package hzx.one;
                        
 import android.app.Activity;
 import android.app.AlertDialog;
 import android.app.AlertDialog.Builder;
 import android.content.DialogInterface;
 import android.os.Bundle;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
 import android.widget.Button;
 import android.widget.Toast;
                        
 public class HzxDialogActivity extends Activity {
     private Button button1;
     private Button button2;
     private Button button3;
     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.main);
         button1=(Button) findViewById(R.id.button1);
         button1.setText("默认dialog");
         button1.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {showDialog1();}});
         button2=(Button) findViewById(R.id.button2);
         button2.setText("自定义dialog");
         button2.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {showDialog2();}});
         button3=(Button) findViewById(R.id.button3);
         button3.setText("自定义toast");
         button3.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {
                 Toast toast=new Toast(HzxDialogActivity.this);
                 //三种方法定义LayoutInflater
                 //LayoutInflater li=getLayoutInflater();
                 //LayoutInflater li = LayoutInflater.from(HzxDialogActivity.this);
                 LayoutInflater li = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
                                        
                 View view=li.inflate(R.layout.dialog, (ViewGroup)findViewById(R.id.dialog));
                 toast.setView(view);
                 toast.setDuration(Toast.LENGTH_SHORT);
                 toast.setGravity(0, -20, 20);
                 toast.show();
             }});
     }
     private void showDialog1() {
         AlertDialog.Builder builder=new Builder(this);
         builder.setMessage("are you sure to exit ?");
         builder.setTitle("exit or not ");
         builder.setPositiveButton("yes", new DialogInterface.OnClickListener() {
             @Override
             public void onClick(DialogInterface dialog, int which) {dialog.dismiss();HzxDialogActivity.this.finish();}});
         builder.setNegativeButton("no", new DialogInterface.OnClickListener() {
             @Override
             public void onClick(DialogInterface dialog, int which) {dialog.dismiss();}});
         builder.create().show();
     }
     private void showDialog2(){
         AlertDialog.Builder builder=new Builder(this);
         LayoutInflater li=getLayoutInflater();
         View v=li.inflate(R.layout.dialog, (ViewGroup)findViewById(R.id.dialog));
         builder.setView(v);
         builder.setPositiveButton("yes", new DialogInterface.OnClickListener() {
             @Override
             public void onClick(DialogInterface dialog, int which) {dialog.dismiss();HzxDialogActivity.this.finish();}});
         builder.setNegativeButton("no", new DialogInterface.OnClickListener() {
             @Override
             public void onClick(DialogInterface dialog, int which) {dialog.dismiss();}});
         builder.create().show();
     }
 }


main.xml布局文件

<?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent" android:orientation="vertical">
 <TextView 
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:text="@string/hello"
     />
 <Button android:text="Button" android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
 <Button android:text="Button" android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
 <Button android:text="Button" android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
 </LinearLayout>


dialog.xml布局文件:

<?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"
     android:orientation="horizontal"
     android:id="@+id/dialog">
                
     <ImageView android:id="@+id/p_w_picpathView1" android:layout_height="wrap_content" android:src="@drawable/icon" android:layout_width="wrap_content"></ImageView>
     <TextView android:layout_height="wrap_content" android:text="@string/hello" android:layout_width="wrap_content" android:id="@+id/textView1"></TextView>
 </LinearLayout>


演示结果如下:

LayoutInflater的使用:动态加载布局_android

默认dialog:

LayoutInflater的使用:动态加载布局_LayoutInflater_02

自定义dialog:

LayoutInflater的使用:动态加载布局_android_03

自定义toast:

LayoutInflater的使用:动态加载布局_android_04


参考:http://www.cnblogs.com/zhukoo/archive/2011/09/22/2184798.html