Android高手進階教程(五)之----Android 中LayoutInflater的使用! (轉)

原創作品,允許轉載,轉載時請務必以超鏈接形式標明文章 原始出處 、作者信息和本聲明。否則將追究法律責任。http://weizhulin.blog.51cto.com/1556324/311450
大家好我們這一節講的是LayoutInflater的使用,在實際開發種LayoutInflater這個類還是非常有用的,它的作用類似於 findViewById(),
不同點是LayoutInflater是用來找layout下xml佈局文件,並且實例化!而findViewById()是找具體xml下的具體 widget控件(如:Button,TextView等)。
爲了讓大家容易理解我做了一個簡單的Demo,主佈局main.xml裏有一個TextView和一個Button,當點擊Button,出現 Dialog,而這個Dialog的佈局方式是我們在layout目錄下定義的custom_dialog.xml文件(裏面左右分佈,左邊 ImageView,右邊TextView)。
效果圖如下:
 
下面我將詳細的說明Demo的實現過程:
1、新建一個 Android工程,我們命名爲LayoutInflaterDemo.
2、修改main.xml佈局,裏面主要在原來基礎上增加了一個Button.代碼如下:
  1. view plaincopy to clipboardprint?  
  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 >     
  22. <? xml   version = "1.0"    
  23. encoding = "utf-8" ?>  
  24. < LinearLayout    
  25. xmlns:android = "http://schemas.android.com/apk/res/android"  
  26.      android:orientation = "vertical"  
  27.      android:layout_width = "fill_parent"  
  28.      android:layout_height = "fill_parent"  
  29.      >  
  30. < TextView     
  31.      android:layout_width = "fill_parent"    
  32.      android:layout_height = "wrap_content"    
  33.      android:text = "@string/hello"  
  34.      />  
  35. < Button  
  36.   android:id = "@+id/button"  
  37.   android:layout_width = "wrap_content"  
  38.   android:layout_height = "wrap_content"  
  39.   android:text = "ShowCustomDialog"  
  40.   />  
  41. </ LinearLayout >  
 
3.定義對話框的佈局方式,我們在layout目錄下,新建一個名爲 custom_dialog.xml文件具體代碼如下:
  1. view plaincopy to clipboardprint?  
  2. <? xml   version = "1.0"       
  3. encoding = "utf-8" ?>      
  4. < LinearLayout       
  5. xmlns:android = "http://schemas.android.com/apk/res/android"     
  6.                android:orientation = "horizontal"     
  7.                android:layout_width = "fill_parent"     
  8.                android:layout_height = "fill_parent"     
  9.                android:padding = "10dp"     
  10.                >      
  11.      < ImageView   android:id = "@+id/image"     
  12.                 android:layout_width = "wrap_content"     
  13.                 android:layout_height = "fill_parent"     
  14.                 android:layout_marginRight = "10dp"     
  15.                 />      
  16.      < TextView   android:id = "@+id/text"     
  17.                android:layout_width = "wrap_content"     
  18.                android:layout_height = "fill_parent"     
  19.                android:textColor = "#FFF"     
  20.                />      
  21. </ LinearLayout >     
  22. <? xml   version = "1.0"    
  23. encoding = "utf-8" ?>  
  24. < LinearLayout    
  25. xmlns:android = "http://schemas.android.com/apk/res/android"  
  26.                android:orientation = "horizontal"  
  27.                android:layout_width = "fill_parent"  
  28.                android:layout_height = "fill_parent"  
  29.                android:padding = "10dp"  
  30.                >  
  31.      < ImageView   android:id = "@+id/image"  
  32.                 android:layout_width = "wrap_content"  
  33.                 android:layout_height = "fill_parent"  
  34.                 android:layout_marginRight = "10dp"  
  35.                 />  
  36.      < TextView   android:id = "@+id/text"  
  37.                android:layout_width = "wrap_content"  
  38.                android:layout_height = "fill_parent"  
  39.                android:textColor = "#FFF"  
  40.                />  
  41. </ LinearLayout >  
 
4.修改主程序LayouInflaterDemo.java代碼如下:
 
  1. view plaincopy to clipboardprint?  
  2. package  com.android.tutor;     
  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. public   class  LayoutInflaterDemo  extends  Activity  implements       
  14. OnClickListener {     
  15.          
  16.      private  Button button;     
  17.      public   void  onCreate(Bundle savedInstanceState) {     
  18.          super .onCreate(savedInstanceState);     
  19.         setContentView(R.layout.main);     
  20.              
  21.         button = (Button)findViewById(R.id.button);     
  22.         button.setOnClickListener( this );     
  23.     }     
  24.      @Override     
  25.      public   void  onClick(View v) {     
  26.              
  27.         showCustomDialog();     
  28.     }     
  29.          
  30.      public   void  showCustomDialog()     
  31.     {     
  32.         AlertDialog.Builder builder;     
  33.         AlertDialog alertDialog;     
  34.         Context mContext = LayoutInflaterDemo. this ;     
  35.              
  36.          //下面倆種方法都可以     
  37.          ////LayoutInflater inflater = getLayoutInflater();     
  38.         LayoutInflater inflater = (LayoutInflater)      
  39. mContext.getSystemService(LAYOUT_INFLATER_SERVICE);     
  40.         View layout = inflater.inflate(R.layout.custom_dialog, null );     
  41.         TextView text = (TextView) layout.findViewById(R.id.text);     
  42.         text.setText( "Hello, Welcome to Mr Wei's blog!" );     
  43.         ImageView image = (ImageView) layout.findViewById(R.id.image);     
  44.         image.setImageResource(R.drawable.icon);     
  45.         builder =  new  AlertDialog.Builder(mContext);     
  46.         builder.setView(layout);     
  47.         alertDialog = builder.create();     
  48.         alertDialog.show();     
  49.     }     
  50. }    
  51. package  com.android.tutor;  
  52. import  android.app.Activity;  
  53. import  android.app.AlertDialog;  
  54. import  android.content.Context;  
  55. import  android.os.Bundle;  
  56. import  android.view.LayoutInflater;  
  57. import  android.view.View;  
  58. import  android.view.View.OnClickListener;  
  59. import  android.widget.Button;  
  60. import  android.widget.ImageView;  
  61. import  android.widget.TextView;  
  62. public   class  LayoutInflaterDemo  extends  Activity  implements    
  63. OnClickListener {  
  64.       
  65.   private  Button button;  
  66.      public   void  onCreate(Bundle savedInstanceState) {  
  67.          super .onCreate(savedInstanceState);  
  68.         setContentView(R.layout.main);  
  69.           
  70.         button = (Button)findViewById(R.id.button);  
  71.         button.setOnClickListener( this );  
  72.     }  
  73.   @Override  
  74.   public   void  onClick(View v) {  
  75.     
  76.   showCustomDialog();  
  77.  }  
  78.    
  79.   public   void  showCustomDialog()  
  80.  {  
  81.   AlertDialog.Builder builder;  
  82.   AlertDialog alertDialog;  
  83.   Context mContext = LayoutInflaterDemo. this ;  
  84.     
  85.    //下面倆種方法都可以  
  86.    ////LayoutInflater inflater = getLayoutInflater();  
  87.   LayoutInflater inflater = (LayoutInflater)   
  88. mContext.getSystemService(LAYOUT_INFLATER_SERVICE);  
  89.   View layout = inflater.inflate(R.layout.custom_dialog, null );  
  90.   TextView text = (TextView) layout.findViewById(R.id.text);  
  91.   text.setText( "Hello, Welcome to Mr Wei's blog!" );  
  92.   ImageView image = (ImageView) layout.findViewById(R.id.image);  
  93.   image.setImageResource(R.drawable.icon);  
  94.   builder =  new  AlertDialog.Builder(mContext);  
  95.   builder.setView(layout);  
  96.   alertDialog = builder.create();  
  97.   alertDialog.show();  
  98.  }  
  99. }   
5、最後執行之,點擊Button,將得到上述效果。
 好今天就到此爲止,睡覺了,大家有什麼不明白的請留言~謝謝!
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章