Android的LayoutInflater

在 实际开发中LayoutInflater这个类还是非常有用的,它的作用类似于findViewById()。不同点是LayoutInflater是用 来找res/layout/下的xml布局文件,并且实例化;而findViewById()是找xml布局文件下的具体widget控件(如 Button、TextView等)。
具体作用:
1、对于一个没有被载入或者想要动态载入的界面,都需要使用LayoutInflater.inflate()来载入;

2、对于一个已经载入的界面,就可以使用Activiyt.findViewById()方法来获得其中的界面元素。

LayoutInflater 是一个抽象类,在文档中如下声明:

publicabstractclass LayoutInflater extends Object 



获得 LayoutInflater 实例的三种方式

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

2.LayoutInflater localinflater =(LayoutInflater)context.getSystemService

                                                (Context.LAYOUT_INFLATER_SERVICE);

3. LayoutInflater inflater = LayoutInflater.from(context);  



其实,这三种方式本质是相同的,从源码中可以看出:这三种方式最终本质是都是调用的Context.getSystemService()。

下面是一个Demo

  1. main.xml
  2. <?xml version="1.0"      
  3. encoding="utf-8"?>     
  4. <LinearLayout      
  5. xmlns:android="http://schemas.android.com/apk/res/android"    
  6.     android:orientation="vertical"    
  7.     android:layout_width="fill_parent"    
  8.     android:layout_height="fill_parent"    
  9.     >     
  10. <TextView       
  11.     android:layout_width="fill_parent"      
  12.     android:layout_height="wrap_content"      
  13.     android:text="@string/hello"    
  14.     />     
  15. <Button     
  16.     android:id="@+id/button"    
  17.     android:layout_width="wrap_content"    
  18.     android:layout_height="wrap_content"    
  19.     android:text="ShowCustomDialog"    
  20.     />     
  21. </LinearLayout>  
复制代码

定义对话框的布局方式custom_dialog.xml

  1. <?xml version="1.0"      
  2. encoding="utf-8"?>     
  3. <LinearLayout      
  4. xmlns:android="http://schemas.android.com/apk/res/android"    
  5.               android:orientation="horizontal"    
  6.               android:layout_width="fill_parent"    
  7.               android:layout_height="fill_parent"    
  8.               android:padding="10dp"    
  9.               >     
  10.     <ImageView android:id="@+id/image"    
  11.                android:layout_width="wrap_content"    
  12.                android:layout_height="fill_parent"    
  13.                android:layout_marginRight="10dp"    
  14.                />     
  15.     <TextView android:id="@+id/text"    
  16.               android:layout_width="wrap_content"    
  17.               android:layout_height="fill_parent"    
  18.               android:textColor="#FFF"    
  19.               />     
  20. </LinearLayout>   
复制代码

Activity代码

  1. package com.android.tutor;  
  2. import android.app.Activity;  
  3. import android.app.AlertDialog;  
  4. import android.content.Context;  
  5. import android.os.Bundle;  
  6. import android.view.LayoutInflater;  
  7. import android.view.View;  
  8. import android.view.View.OnClickListener;  
  9. import android.widget.Button;  
  10. import android.widget.ImageView;  
  11. import android.widget.TextView;  
  12. public class LayoutInflaterDemo extends Activity implements   
  13. OnClickListener {  
  14.       
  15. private Button button;  
  16.     public void onCreate(Bundle savedInstanceState) {  
  17.         super.onCreate(savedInstanceState);  
  18.         setContentView(R.layout.main);  
  19.           
  20.         button = (Button)findViewById(R.id.button);  
  21.         button.setOnClickListener(this);  
  22.     }  
  23. @Override 
  24. public void onClick(View v) {  
  25.     
  26.   showCustomDialog();  
  27. }  
  28.    
  29. public void showCustomDialog()  
  30. {  
  31.   AlertDialog.Builder builder;  
  32.   AlertDialog alertDialog;  
  33.   Context mContext = LayoutInflaterDemo.this;  
  34.     
  35.   //下面俩种方法都可以  
  36.   //LayoutInflater inflater = getLayoutInflater();  
  37.   LayoutInflater inflater = (LayoutInflater)   mContext.getSystemService(LAYOUT_INFLATER_SERVICE);  
  38.   View layout = inflater.inflate(R.layout.custom_dialog,null);  //返回值为view
  39.   TextView text = (TextView) layout.findViewById(R.id.text);  
  40.   text.setText("Hello, Welcome to Mr Wei's blog!");  
  41.   ImageView image = (ImageView) layout.findViewById(R.id.image);  
  42.   image.setImageResource(R.drawable.icon);  
  43.   builder = new AlertDialog.Builder(mContext);  
  44.   builder.setView(layout);  
  45.   alertDialog = builder.create();  
  46.   alertDialog.show();  
  47. }  
  48. }   
复制代码
运行效果:

posted @ 2015-05-27 15:37  techfox  阅读(100)  评论(0编辑  收藏  举报