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

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

View Code
复制代码
 1 package hzx.one;
 2 
 3 import android.app.Activity;
 4 import android.app.AlertDialog;
 5 import android.app.AlertDialog.Builder;
 6 import android.content.DialogInterface;
 7 import android.os.Bundle;
 8 import android.view.LayoutInflater;
 9 import android.view.View;
10 import android.view.ViewGroup;
11 import android.widget.Button;
12 import android.widget.Toast;
13 
14 public class HzxDialogActivity extends Activity {
15     private Button button1;
16     private Button button2;
17     private Button button3;
18     @Override
19     public void onCreate(Bundle savedInstanceState) {
20         super.onCreate(savedInstanceState);
21         setContentView(R.layout.main);
22         button1=(Button) findViewById(R.id.button1);
23         button1.setText("默认dialog");
24         button1.setOnClickListener(new View.OnClickListener() {
25             @Override
26             public void onClick(View v) {showDialog1();}});
27         button2=(Button) findViewById(R.id.button2);
28         button2.setText("自定义dialog");
29         button2.setOnClickListener(new View.OnClickListener() {
30             @Override
31             public void onClick(View v) {showDialog2();}});
32         button3=(Button) findViewById(R.id.button3);
33         button3.setText("自定义toast");
34         button3.setOnClickListener(new View.OnClickListener() {
35             @Override
36             public void onClick(View v) {
37                 Toast toast=new Toast(HzxDialogActivity.this);
38                 //三种方法定义LayoutInflater
39                 //LayoutInflater li=getLayoutInflater();
40                 //LayoutInflater li = LayoutInflater.from(HzxDialogActivity.this); 
41                 LayoutInflater li = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
42                 
43                 View view=li.inflate(R.layout.dialog, (ViewGroup)findViewById(R.id.dialog));
44                 toast.setView(view);
45                 toast.setDuration(Toast.LENGTH_SHORT);
46                 toast.setGravity(0, -20, 20);
47                 toast.show();
48             }});
49     }
50     private void showDialog1() {
51         AlertDialog.Builder builder=new Builder(this);
52         builder.setMessage("are you sure to exit ?");
53         builder.setTitle("exit or not ");
54         builder.setPositiveButton("yes", new DialogInterface.OnClickListener() {
55             @Override
56             public void onClick(DialogInterface dialog, int which) {dialog.dismiss();HzxDialogActivity.this.finish();}});
57         builder.setNegativeButton("no", new DialogInterface.OnClickListener() {
58             @Override
59             public void onClick(DialogInterface dialog, int which) {dialog.dismiss();}});
60         builder.create().show();
61     }
62     private void showDialog2(){
63         AlertDialog.Builder builder=new Builder(this);
64         LayoutInflater li=getLayoutInflater();
65         View v=li.inflate(R.layout.dialog, (ViewGroup)findViewById(R.id.dialog));
66         builder.setView(v);
67         builder.setPositiveButton("yes", new DialogInterface.OnClickListener() {
68             @Override
69             public void onClick(DialogInterface dialog, int which) {dialog.dismiss();HzxDialogActivity.this.finish();}});
70         builder.setNegativeButton("no", new DialogInterface.OnClickListener() {
71             @Override
72             public void onClick(DialogInterface dialog, int which) {dialog.dismiss();}});
73         builder.create().show();
74     }
75 }
复制代码



main.xml布局文件

复制代码
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="fill_parent"
 4     android:layout_height="fill_parent" android:orientation="vertical">
 5 <TextView  
 6     android:layout_width="fill_parent" 
 7     android:layout_height="wrap_content" 
 8     android:text="@string/hello"
 9     />
10 <Button android:text="Button" android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
11 <Button android:text="Button" android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
12 <Button android:text="Button" android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
13 </LinearLayout>
复制代码

dialog.xml布局文件

复制代码
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="fill_parent"
 4     android:layout_height="fill_parent"
 5     android:orientation="horizontal"
 6     android:id="@+id/dialog">
 7     
 8     <ImageView android:id="@+id/imageView1" android:layout_height="wrap_content" android:src="@drawable/icon" android:layout_width="wrap_content"></ImageView>
 9     <TextView android:layout_height="wrap_content" android:text="@string/hello" android:layout_width="wrap_content" android:id="@+id/textView1"></TextView>
10 </LinearLayout>
复制代码

演示结果如下:

默认dialog:

自定义dialog:

自定义toast: