在 实际开发中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

main.xml

<?xml version="1.0"

encoding="utf-8"?>

<LinearLayout

xmlns:android="http://schemas.android.com/apk/res/android"

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

>

<TextView

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/hello"

/>

<Button

android:id="@+id/button"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="ShowCustomDialog"

/>

</LinearLayout>

复制代码

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

<?xml version="1.0"

encoding="utf-8"?>

<LinearLayout

xmlns:android="http://schemas.android.com/apk/res/android"

android:orientation="horizontal"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:padding="10dp"

>

<ImageView android:id="@+id/p_w_picpath"

android:layout_width="wrap_content"

android:layout_height="fill_parent"

android:layout_marginRight="10dp"

/>

<TextView android:id="@+id/text"

android:layout_width="wrap_content"

android:layout_height="fill_parent"

android:textColor="#FFF"

/>

</LinearLayout>

复制代码

Activity代码

package com.android.tutor;

import android.app.Activity;

import android.app.AlertDialog;

import android.content.Context;

import android.os.Bundle;

import android.view.LayoutInflater;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.ImageView;

import android.widget.TextView;

public class LayoutInflaterDemo extends Activity implements

OnClickListener {


private Button button;

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);


button = (Button)findViewById(R.id.button);

button.setOnClickListener(this);

}

@Override

public void onClick(View v) {


showCustomDialog();

}


public void showCustomDialog()

{

AlertDialog.Builder builder;

AlertDialog alertDialog;

Context mContext = LayoutInflaterDemo.this;


//下面俩种方法都可以

//LayoutInflater inflater = getLayoutInflater();

LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);

View layout = inflater.inflate(R.layout.custom_dialog,null);//返回值为view

TextView text = (TextView) layout.findViewById(R.id.text);

text.setText("Hello, Welcome to Mr Wei's blog!");

ImageView p_w_picpath = (ImageView) layout.findViewById(R.id.p_w_picpath);

p_w_picpath.setImageResource(R.drawable.icon);

builder = new AlertDialog.Builder(mContext);

builder.setView(layout);

alertDialog = builder.create();

alertDialog.show();

}

}


复制代码

运行效果: