Android开发心得:LayoutInflater及inflate方法


引言:

Android开发心得笔记,先看一段代码

public void OnCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

这个是Activity创建时执行的方法,其中最后一句大家应该都见过,这个加载布局的最简单的方法,但是如果,你需要动态加载布局,那就需要使用其他办法。

那就是LayoutInfater的inflate方法。

一。获得 LayoutInflater 实例的三种方式

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

2.LayoutInflater localinflater =(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

3. LayoutInflater inflater = LayoutInflater.from(context);

二。inflate方法

public View inflate(int Resourece,ViewGroup root)

作用:填充一个新的视图层次结构从指定的XML资源文件中

reSource:View的layout的ID

root: 生成的层次结构的根视图

return 填充的层次结构的根视图

如果参数root提供了,那么root就是根视图;否则填充的XML文件的根就是根视图。其余几个重载的inflate函数类似。

在View类中,也有inflate方法,相关的可以看下这篇文章

结束语:

这段代码和之前说的那最后一句是一个意思:

LayoutInflater inflate = LayoutInflater.from(this);
View view = inflate.inflate(R.layout.main,null);
setContentView(view);

Android 4.4.4 发布下载

最简单的Ubuntu Touch & Android 双系统安装方式

在Nexus上实现Ubuntu和Android 4.4.2 双启动

Ubuntu 14.04 配置 Android SDK 开发环境

64位Ubuntu 11.10下Android开发环境的搭建(JDK+Eclipse+ADT+Android SDK详细)

Ubuntu 14.04 x64配置Android 4.4 kitkat编译环境的方法

Ubuntu 12.10 x64 安装 Android SDK

相关内容