转自:http://dev.10086.cn/cmdn/wiki/index.php?doc-view-6066.html
Inflater英文意思是膨胀,在Android中应该是扩展的意思吧。
LayoutInflater的作用类似于 findViewById(),不同点是LayoutInflater是用来找layout文件夹下的xml布局文件,并且实例化!而 findViewById()是找具体某一个xml下的具体 widget控件(如:Button,TextView等)。

她可以有很多地方可以使用,如BaseAdapter的getView中,自定义Dialog中取得view中的组件widget等等。

它的用法有2种:

第一种方式:

package org.hwq.layoutinflater;import android.app.Activity;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;import android.widget.EditText;import android.widget.Toast;public class Main extends Activity {EditText et;    /** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        //LayoutInflater第一种使用方式        LayoutInflater inflater = LayoutInflater.from(this);        //取出main.xml实例的view        View view = inflater.inflate(R.layout.main, null);        //这里以前我们是使用了:this.findViewById(R.id.editText1);        et = (EditText) view.findViewById(R.id.editText1);        Toast.makeText(this, et.getHint(),0).show();    }}


第二种方式:

package org.hwq.layoutinflater;import android.app.Activity;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;import android.widget.EditText;import android.widget.Toast;public class Main extends Activity {EditText et;    /** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        //LayoutInflater第二种使用方式        LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);        //取出main.xml实例的view        View view = inflater.inflate(R.layout.main, null);        //这里以前我们是使用了:this.findViewById(R.id.editText1);        et = (EditText) view.findViewById(R.id.editText1);        Toast.makeText(this, et.getHint(),0).show();    }}


对比发现其实就改了一行。
LayoutInflater inflater = LayoutInflater.from(this);
LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);

查看源代码你会发现LayoutInflater.from()方法调用如下:
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;    }


更多相关文章

  1. Android(安卓)为某个App 添加/移除 启动快捷方式、书签快捷方式
  2. android BroadcastReceiver 注册方式
  3. 在官网途径无法下载到android 源码时,,你可通过这种方式去下载
  4. RelativeLayout常用属性介绍及实例讲解
  5. android socket 编程实例
  6. Android(安卓)2.0 API 读取通讯录中电话号码的实例 (转)
  7. Android(安卓)Spinner与适配器模式详解及实例代码
  8. Android(安卓)IPC机制之 Android的各种IPC方式
  9. Android之LayoutInflater加载布局及原理分析

随机推荐

  1. Android 把从网络获取的图片缓存到内存中
  2. android 设置全屏的三种方式
  3. Android用SharedPreferences记住用户名
  4. Android简单实现音乐播放器
  5. [置顶] Linux下安装配置Android开发环境
  6. android遮罩阴影对话框的实现
  7. android圆角按钮
  8. Android(安卓)中 Intent 的使用
  9. android listview 的一些属性
  10. 怀恋Button 样式 怕再次忘记