Inflate()

 LayoutInflater作用是将layout的xml布局文件实例化为View类对象,LayoutInflater的作用类似于 findViewById(),不同点是LayoutInflater是用来找layout文件夹下的xml布局文件,并且实例化!而 findViewById()是找具体某一个xml下的具体 widget控件(如:Button,TextView等)。

   二.获得 LayoutInflater 实例的三种方式

          1.LayoutInflater inflater = getLayoutInflater();                //调用Activity的getLayoutInflater()

          2.LayoutInflater inflater = LayoutInflater.from(this);       

          3.LayoutInflater inflater = (LayoutInflater)Context.getSystemService(LAYOUT_INFLATER_SERVICE);

          

          其实,这三种方式本质是相同的,从源码中可以看出:

          getLayoutInflater():

          Activity 的 getLayoutInflater() 方法是调用 PhoneWindow 的getLayoutInflater()方法,看一下该源代码:

            

public PhoneWindow(Context context) {  
        super(context);  
        mLayoutInflater = LayoutInflater.from(context);  
}  

         可以看出它其实是调用 LayoutInflater.from(context)。

 

         LayoutInflater.from(context):

复制代码
public static LayoutInflater from(Context context) {   
    LayoutInflater LayoutInflater =   
            (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);   
    if (LayoutInflater == null) {   
        throw new AssertionError("LayoutInflater not found.");   
    }   
    return LayoutInflater;   
} 
复制代码

 

       可以看出它其实调用 context.getSystemService()。

 

      结论:所以这三种方式最终本质是都是调用的Context.getSystemService()。

 

  因此如果你的Activity里如果用到别的layout,比如对话框上的layout,你还要设置对话框上的layout里的组件(像图片ImageView,文字TextView)上的内容,你就必须用inflate()先将对话框上的layout找出来,然后再用这个layout对象去找到它上面的组件,如:
  
  View view=View.inflate(this,R.layout.dialog_layout,null);
  
  TextView dialogTV=(TextView)view.findViewById(R.id.dialog_tv);
  
  dialogTV.setText("abcd");

package com.bivin;

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 MainActivity 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 = MainActivity.this;

LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.custom_dialog, null);
TextView text = (TextView) layout.findViewById(R.id.text);
text.setText("Hello, Welcome to Mr Wei's blog!");
ImageView image = (ImageView) layout.findViewById(R.id.image);
image.setImageResource(R.drawable.icon);
builder = new AlertDialog.Builder(mContext);
builder.setView(layout);
alertDialog = builder.create();
alertDialog.show();
}
}
View Code

posted @ 2013-10-20 21:30  xiangtailiang  阅读(998)  评论(0编辑  收藏  举报