Android开发之LayoutInflater的三种实例化方式

来源:互联网 发布:淘宝漫步者官方旗舰店 编辑:程序博客网 时间:2024/05/21 10:43

LayoutInflater作用是将layout的xml布局文件实例化为View类对象。

实现LayoutInflater的实例化共有3种方法:

(1).通过SystemService获得

  LayoutInflaterinflater =     (LayoutInflater)context.getSystemServices(Context.LAYOUT_INFLATER_SERVICES);  Viewview = inflater.inflate(R.layout.main, null);

(2).从给定的context中获得

 LayoutInflaterinflater = LayoutInflater.from(context); Viewview = inflater.inflate(R.layout.mian, null);

(3).在Activity中调用

 LayoutInflaterinflater =getLayoutInflater();(在Activity中可以使用,实际上是View子类下window的一个函数) Viewlayout = inflater.inflate(R.layout.main, null);

其实这三种方法从源码看,实现是一样的

作用:填充一个新的视图层次结构从指定的XML资源文件中
reSource:View的layout的ID
root: 生成的层次结构的根视图
return 填充的层次结构的根视图。

0 0