Android开发教程:LayoutInflater的应用


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.     }  

效果图:

  • 1
  • 2
  • 下一页

相关内容