转自: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;
}
分享到:
相关推荐
Android 中LayoutInflater的使用
android中LayoutInflater的使用.pdf
Android 中LayoutInflater(布局加载器)之实战篇 博客的Demo 博客地址: http://blog.csdn.net/l540675759/article/details/78112989 两种方式实现小红书的引导页: (1)自定义View (2)自定义LayoutInflater....
测试:Android 中LayoutInflater的使用 注意:Aj_04是用了调用另外一个界面,要注意调用的方法, 还一定还要在AndroidManifest.xml 中加上呢句:<activity android:name="LayoutInflaterDemo"></activity>
主要介绍了Android 中LayoutInflater.inflate()方法的介绍的相关资料,希望通过本文大家能掌握这部分内容,需要的朋友可以参考下
主要给大家介绍了关于Android中LayoutInflater.inflater()的正确打开方式,文中通过示例代码介绍的非常详细,需要的朋友可以参考借鉴,下面随着小编来一起学习学习吧
主要介绍了Android LayoutInflater分析的相关资料,需要的朋友可以参考下
Android高手进阶教程之----Android 中LayoutInflater的使用! .doc Android高手进阶教程之----Android 中MenuInflater的使用(布局定义菜单)! .doc Android高手进阶教程之----Android 中Preferences的使用! .doc ...
NULL 博文链接:https://yufeng-zhu.iteye.com/blog/1676207
down-test-Android 获得 LayoutInflater 实例的三种方式
LayoutInflater Layout 与 Inflater[扩充]两个单词的合并,可以理解为:当前Activity 布局的扩充.
android inflate demo,方便新手使用,下载导入android studio就可直接运行,简单明了的展现inflate不同参数的区别
LayoutInflater类在我们日常开发中经常会用到,最近在使用中就遇到了一些问题,所有下面这篇文章主要给大家总结了关于Android中使用LayoutInflater要注意的一些坑,希望通过这篇能让大家避免走一些弯路,需要的朋友...
通过LayoutInflater.Factory实现主题切换功能,详见:http://blog.csdn.net/llew2011
主要介绍了Android开发之获取LayoutInflater对象的方法,结合实例形式总结分析了Android获取LayoutInflater对象的常用技巧,需要的朋友可以参考下
Android LayoutInflater加载布局详解 对于有一定Android开发经验的同学来说,一定使用过LayoutInflater.inflater()来加载布局文件,但并不一定去深究过它的原理,比如 1.LayoutInflater为什么可以加载layout文件? ...