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:

[xhtml] view plaincopy
 
  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.         <TextView    
  7.             android:layout_width="fill_parent"   
  8.             android:layout_height="wrap_content"   
  9.             android:text="@string/hello"/>  
  10.         <Button  
  11.             android:id="@+id/button"  
  12.             android:layout_width="wrap_content"  
  13.             android:layout_height="wrap_content"  
  14.             android:text="ShowCustomDialog"/>  
  15. </LinearLayout>  

custom_dialog.xml:

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

TestLayoutInflater.java:

[c-sharp] view plaincopy
 
  1. package com.android.test;  
  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 TestLayoutInflater 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 = TestSlidingDrawer.this;  
  34.           
  35.         //Three ways are OK.  
  36.         LayoutInflater inflater = getLayoutInflater();  //Activity.getLayoutInflater() or Window.getLayoutInflater().  
  37. //      LayoutInflater inflater = LayoutInflater.from(this);    //Obtains the LayoutInflater from the given context.  
  38. //      LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);  
  39.           
  40.         View layout = inflater.inflate(R.layout.custom_dialog,null);  
  41.           
  42.         TextView text = (TextView) layout.findViewById(R.id.text);  
  43.         text.setText("Hello, This is a LayoutInflater Demo");  
  44.         ImageView image = (ImageView) layout.findViewById(R.id.image);  
  45.         image.setImageResource(R.drawable.icon);  
  46.           
  47.         builder = new AlertDialog.Builder(mContext);  
  48.         builder.setView(layout);  
  49.         alertDialog = builder.create();  
  50.         alertDialog.show();  
  51.           
  52.         //Using AlertDialog directly.  
  53. //      alertDialog = new AlertDialog(mContext) {  
  54. //      };  
  55. //      alertDialog.setView(layout);  
  56. //      alertDialog.show();  
  57.     }  
  58. }  

几点说明:

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

转载自:http://blog.csdn.net/zhangqijie001/article/details/5835838

原文地址:https://www.cnblogs.com/ambitious-kevin/p/4452171.html