【Android进阶学习】LayoutInflater的应用

来源:互联网 发布:怎么在淘宝买便宜东西 编辑:程序博客网 时间:2024/05/21 06:45
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://liangruijun.blog.51cto.com/3061169/750495

LayoutInflater在Android中是“扩展”的意思,作用类似findViewById( ),它在Android开发中的作用是很大的。LayoutInflater经常在BaseAdapter的getView方法中用到,用来获取整个View并返回。

LayoutInflater与findViewById( )的不同点:

  • LayoutInflater是将XML中的Layout转换为View放入.java代码中
  • findViewById()是找具体xml下的具体组件(如:Button,TextView,ImageView等)。

获得LayoutInflater的三种方法:

第一种:

  1. LayoutInflater inflater = LayoutInflater.from(this);  
  2. View layout = inflater.inflate(R.layout.main, null); 

第二种:

  1. LayoutInflater inflater = getLayoutInflater();  
  2. View layout = inflater.inflate(R.layout.main, null); 

第三种:

  1. LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);  
  2. View layout = inflater.inflate(R.layout.main, null); 

getSystemService()是Android很重要的一个API,它是Activity的一个方法,根据传入 的NAME来取得对应的Object,然后转换成相应的服务对象。以下介绍系统相应的服务。其中LAYOUT_INFLATER_SERVICE返回的对象是LayoutInflater,作用是取得XML定义的View。

第一个实例:

main.xml

 

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  3.     android:orientation="vertical" 
  4.     android:layout_width="fill_parent" 
  5.     android:layout_height="fill_parent" 
  6.     > 
  7. <EditText 
  8.     android:id="@+id/text" 
  9.     android:layout_width="fill_parent" 
  10.     android:layout_height="wrap_content" 
  11.     /> 
  12. <Button   
  13.     android:id="@+id/btn1" 
  14.     android:layout_width="fill_parent" 
  15.     android:layout_height="wrap_content" 
  16.     /> 
  17. <Button   
  18.     android:id="@+id/btn2" 
  19.     android:layout_width="fill_parent" 
  20.     android:layout_height="wrap_content" 
  21.     /> 
  22. </LinearLayout> 

MainActivity.java

  1. package com.lingdududu.test;  
  2.  
  3. import android.app.Activity;  
  4. import android.graphics.Color;  
  5. import android.os.Bundle;  
  6. import android.view.LayoutInflater;  
  7. import android.view.View;  
  8. import android.widget.Button;  
  9. import android.widget.EditText;  
  10.  
  11. public class MainActivity extends Activity {  
  12.     private EditText etx;  
  13.     private Button confirmBtn;  
  14.     private Button cancleBtn;  
  15.       
  16.     /** Called when the activity is first created. */ 
  17.     @Override 
  18.     public void onCreate(Bundle savedInstanceState) {  
  19.         super.onCreate(savedInstanceState);  
  20.           
  21.         LayoutInflater inflater = getLayoutInflater();  
  22.         View layout = inflater.inflate(R.layout.main, null);  
  23.           
  24.         etx = (EditText)layout.findViewById(R.id.text);  
  25.         etx.setBackgroundColor(Color.WHITE);  
  26.         etx.setHint("请输入你的学号");  
  27.           
  28.         confirmBtn = (Button)layout.findViewById(R.id.btn1);  
  29.         confirmBtn.setText("确定");  
  30.           
  31.         cancleBtn = (Button)layout.findViewById(R.id.btn2);  
  32.         cancleBtn.setText("取消");  
  33.           
  34.         setContentView(layout);     
  35.     }  

效果图:

第二个实例:

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

 main.xml

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  3.     android:orientation="vertical" 
  4.     android:layout_width="fill_parent" 
  5.     android:layout_height="fill_parent" 
  6.     > 
  7. <TextView    
  8.     android:layout_width="fill_parent"   
  9.     android:layout_height="wrap_content"   
  10.     android:text="@string/hello" 
  11.     /> 
  12. <Button   
  13.     android:id="@+id/btn" 
  14.     android:layout_width="fill_parent" 
  15.     android:layout_height="wrap_content" 
  16.     android:text="打开对话框" 
  17.     /> 
  18. </LinearLayout> 

dialog.xml

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <LinearLayout 
  3.   xmlns:android="http://schemas.android.com/apk/res/android" 
  4.   android:orientation="horizontal" 
  5.   android:layout_width="match_parent" 
  6.   android:layout_height="match_parent" 
  7.   > 
  8.   <ImageView   
  9.    android:id="@+id/img" 
  10.    android:layout_width="wrap_content" 
  11.    android:layout_height="wrap_content" 
  12.    />    
  13.   <TextView   
  14.    android:id="@+id/dialog_txt" 
  15.    android:layout_width="wrap_content" 
  16.    android:layout_height="wrap_content" 
  17.    android:text="测试LayoutInflater" 
  18.    /> 
  19. </LinearLayout> 

MainActivity.java

  1. package com.lingdududu.test;  
  2.  
  3. import android.app.Activity;  
  4. import android.app.AlertDialog;  
  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.  
  13. public class MainActivity extends Activity {  
  14.     private Button showBtn;  
  15.     /** Called when the activity is first created. */ 
  16.     @Override 
  17.     public void onCreate(Bundle savedInstanceState) {  
  18.         super.onCreate(savedInstanceState);  
  19.         setContentView(R.layout.main);  
  20.         showBtn = (Button)findViewById(R.id.btn);  
  21.         showBtn.setOnClickListener(new showDialogListener());  
  22.           
  23.     }  
  24.       
  25.     class showDialogListener implements OnClickListener{  
  26.  
  27.         @Override 
  28.         public void onClick(View v) {  
  29.             // TODO Auto-generated method stub  
  30.             showDialog();  
  31.         }  
  32.           
  33.     }  
  34.       
  35.     public void showDialog() {  
  36.         AlertDialog.Builder builder;   
  37.         AlertDialog dialog ;  
  38.           
  39.         LayoutInflater inflater = (LayoutInflater)this.getSystemService(LAYOUT_INFLATER_SERVICE);   
  40.         //LayoutInflater inflater = getLayoutInflater();  
  41.         //LayoutInflater inflater = LayoutInflater.from(this);    
  42.         View layout = inflater.inflate(R.layout.dialog, null);  
  43.           
  44.         TextView tx = (TextView)layout.findViewById(R.id.dialog_txt);  
  45.         tx.setText("测试LayoutInflater");  
  46.         ImageView img = (ImageView)layout.findViewById(R.id.img);  
  47.         img.setImageResource(R.drawable.icon);  
  48.           
  49.         builder = new AlertDialog.Builder(this);     
  50.         builder.setView(layout);    
  51.         dialog = builder.create();  
  52.         dialog.show();  
  53.     }  

效果图:

 

本文出自 “IT的点点滴滴” 博客,请务必保留此出处http://liangruijun.blog.51cto.com/3061169/750495