总结系列-LayoutInflater的inflate函数用法详解
来源:互联网 发布:java编写宿舍管理系统 编辑:程序博客网 时间:2024/06/05 20:06
LayoutInflater的inflate函数用法详解
LayoutInflater作用是将layout的xml布局文件实例化为View类对象。
获取LayoutInflater的方法有如下三种方法:
LayoutInflater inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.main,
null
);
LayoutInflater inflater = LayoutInflater.from(context); (该方法实质就是第一种方法,可参考源代码)
View layout = inflater.inflate(R.layout.main,
null
);
LayoutInflater inflater = getLayoutInflater();(在Activity中可以使用,实际上是View子类下window的一个函数)
View layout = inflater.inflate(R.layout.main,
null
);
一直有点纠结setContentView和inflate的区别找了一些资料。写了个小程序看了下:?
public
class
MyInflate
extends
Activity{
private
TextView tv;
public
void
OnCreate(Bundle savedInstanceState){
super
.onCreate(savedInstanceState);
//setContentView(R.layout.main);
//tv = (TextView) findViewById(R.id.tv);
LayoutInflater inflate = LayoutInflater.from(
this
);
View view = inflate.inflate(R.layout.main,
null
);
setContentView(view);
}
}
上述注释掉的代码和没有注释掉的代码两种情况是相同的。
区别:
setContentView()一旦调用, layout就会立刻显示UI;而inflate只会把Layout形成一个以view类实现成的对象,有需要时再用setContentView(view)显示出来。一般在activity中通过setContentView()将界面显示出来,但是如果在非activity中如何对控件布局设置操作了,这就需要LayoutInflater动态加载。
public View inflate(int Resourece,ViewGroup root)
作用:填充一个新的视图层次结构从指定的XML资源文件中
reSource:View的layout的ID
root: 生成的层次结构的根视图
return 填充的层次结构的根视图。如果参数root提供了,那么root就是根视图;否则填充的XML文件的根就是根视图。
其余几个重载的inflate函数类似。
0 0
- 总结系列-LayoutInflater的inflate函数用法详解
- 总结系列-LayoutInflater的inflate函数用法详解
- 总结系列-LayoutInflater的inflate函数用法详解
- LayoutInflater的inflate函数用法详解
- LayoutInflater的inflate函数用法详解
- LayoutInflater的inflate函数用法详解
- LayoutInflater的inflate函数用法详解
- LayoutInflater的inflate函数用法详解
- LayoutInflater的inflate函数用法详解
- LayoutInflater的inflate函数用法详解
- LayoutInflater的inflate函数用法详解
- LayoutInflater的inflate函数用法详解
- LayoutInflater的inflate函数用法详解
- LayoutInflater的inflate函数用法详解
- LayoutInflater的inflate函数用法详解
- LayoutInflater的inflate函数用法详解
- LayoutInflater的inflate函数用法详解
- LayoutInflater的inflate函数用法详解
- 阿里百川Android应用
- java编码格式
- svn 命令行下常用的几个命令
- mac 终端 常用命令
- 如何在Mac上使用Swift调用C接口开发条形码应用
- 总结系列-LayoutInflater的inflate函数用法详解
- Java的注解机制——Spring自动装配的实现原理
- Android 单元测试(四)测试 Activity和Fragment
- WPF 多线程---- 子线程操作界面主线程的元素
- Ubuntu 一条命令安装LAMP
- 野人学Android基础篇之初探UI控件第二课--ImageView动态赋值
- 网易新闻iOS版使用的开源组件
- javascript表单验证之SQL注入验证
- Parcelable传递图片,复杂对象,对象列表