Android中LayoutInflater实例

LayoutInflater与findViewById的用法有很多可比较之处。

如:他们都可以通过id返回View。

LayoutInflater inflater = getLayoutInflater();

View layout = inflater.inflate(R.layout.custom_dialog,null);

Button button = (Button) findViewById(R.id.button);

不同之处是:LayoutInflater是用来实例化整个布局文件,而findViewById()是实例化布局文中中的View。

下面是一个实例。 主布局main.xml里有一个TextView和一个Button,当点击Button,出现Dialog,而这个Dialog的布局方式是我们在layout目录下定义的custom_dialog.xml文件(里面左右分布,左边 ImageView,右边TextView)。

效果图如下:


源代码如下:

main.xml:
<?xml version="1.0" encoding="utf-8"?> 
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:orientation="vertical" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent"> 
         <TextView    
             android:layout_width="fill_parent"   
             android:layout_height="wrap_content"   
             android:text="@string/hello"/> 
         <Button 
             android:id="@+id/button" 
             android:layout_width="wrap_content" 
             android:layout_height="wrap_content" 
             android:text="ShowCustomDialog"/> 
 </LinearLayout> 
 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:orientation="vertical"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent">
   <TextView 
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:text="@string/hello"/>
   <Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="ShowCustomDialog"/>
 </LinearLayout>
  custom_dialog.xml:
<?xml version="1.0" encoding="utf-8"?> 
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:orientation="horizontal" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:padding="10dp"> 
        <ImageView   
             android:id="@+id/image" 
           android:layout_width="wrap_content" 
           android:layout_height="fill_parent" 
           android:layout_marginRight="10dp"/> 
        <TextView   
             android:id="@+id/text" 
            android:layout_width="wrap_content" 
            android:layout_height="fill_parent" 
            android:textColor="#FFF"/> 
 </LinearLayout> 
 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="horizontal"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:padding="10dp">
     <ImageView
       android:id="@+id/image"
           android:layout_width="wrap_content"
           android:layout_height="fill_parent"
           android:layout_marginRight="10dp"/>
     <TextView
       android:id="@+id/text"
         android:layout_width="wrap_content"
         android:layout_height="fill_parent"
         android:textColor="#FFF"/>
 </LinearLayout>
  TestLayoutInflater.java:
package com.android.test;  
 import android.app.Activity;  
 import android.app.AlertDialog;  
 import android.content.Context;  
 import android.os.Bundle;  
 import android.view.LayoutInflater;  
 import android.view.View;  
 import android.view.View.OnClickListener;  
 import android.widget.Button;  
 import android.widget.ImageView;  
 import android.widget.TextView;  
 public class TestLayoutInflater extends Activity implements   
 OnClickListener {  
       
     private Button button;  
     public void onCreate(Bundle savedInstanceState) {  
         super.onCreate(savedInstanceState);  
         setContentView(R.layout.main);  
           
         button = (Button)findViewById(R.id.button);  
         button.setOnClickListener(this);  
     }  
     @Override  
     public void onClick(View v) {  
           
         showCustomDialog();  
     }  
       
     public void showCustomDialog()  
     {  
         AlertDialog.Builder builder;  
         AlertDialog alertDialog;  
         Context mContext = TestSlidingDrawer.this;  
           
         //Three ways are OK.  
         LayoutInflater inflater = getLayoutInflater();  //Activity.getLayoutInflater() or Window.getLayoutInflater().  
 //      LayoutInflater inflater = LayoutInflater.from(this);    //Obtains the LayoutInflater from the given context.  
 //      LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);  
           
         View layout = inflater.inflate(R.layout.custom_dialog,null);  
           
         TextView text = (TextView) layout.findViewById(R.id.text);  
         text.setText("Hello, This is a LayoutInflater Demo");  
         ImageView image = (ImageView) layout.findViewById(R.id.image);  
         image.setImageResource(R.drawable.icon);  
           
         builder = new AlertDialog.Builder(mContext);  
         builder.setView(layout);  
         alertDialog = builder.create();  
         alertDialog.show();  
           
         //Using AlertDialog directly.  
 //      alertDialog = new AlertDialog(mContext) {  
 //      };  
 //      alertDialog.setView(layout);  
 //      alertDialog.show();  
     }  
 } 
 package com.android.test;
 import android.app.Activity;
 import android.app.AlertDialog;
 import android.content.Context;
 import android.os.Bundle;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.View.OnClickListener;
 import android.widget.Button;
 import android.widget.ImageView;
 import android.widget.TextView;
 public class TestLayoutInflater extends Activity implements
 OnClickListener {
    
  private Button button;
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.main);
        
         button = (Button)findViewById(R.id.button);
         button.setOnClickListener(this);
     }
  @Override
  public void onClick(View v) {
   
   showCustomDialog();
  }
  
  public void showCustomDialog()
  {
   AlertDialog.Builder builder;
   AlertDialog alertDialog;
   Context mContext = TestSlidingDrawer.this;
   
   //Three ways are OK.
   LayoutInflater inflater = getLayoutInflater(); //Activity.getLayoutInflater() or Window.getLayoutInflater().
 //  LayoutInflater inflater = LayoutInflater.from(this); //Obtains the LayoutInflater from the given context.
 //  LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
   
   View layout = inflater.inflate(R.layout.custom_dialog,null);
   
   TextView text = (TextView) layout.findViewById(R.id.text);
   text.setText("Hello, This is a LayoutInflater Demo");
   ImageView image = (ImageView) layout.findViewById(R.id.image);
   image.setImageResource(R.drawable.icon);
   
   builder = new AlertDialog.Builder(mContext);
   builder.setView(layout);
   alertDialog = builder.create();
   alertDialog.show();
   
   //Using AlertDialog directly.
 //  alertDialog = new AlertDialog(mContext) {
 //  };
 //  alertDialog.setView(layout);
 //  alertDialog.show();
  }
 }

几点说明:

LayoutInflater的获取有三种方法:Activity.getLayoutInflater(), LayoutInflater.from(Context), getSystemService(String).
也可以不通过AlertDialog的内部类AlertDialog.Builder来获取AlertDialog对象,AlertDialog也能setView(View).另外,注意:AlertDialog的构造函数有些特殊,需要加{},如果去掉就报“The constructor AlertDialog(Context) is not visible”的错误。