LayoutInflater作用用法
2013-11-06 11:46
253 查看
LayoutInflater作用是将layout的xml布局文件实例化为View类对象。获取LayoutInflater的方法有如下三种:
?
一直有点纠结setContentView和inflate的区别找了一些资料。写了个小程序看了下:
?
上述注释掉的代码和没有注释掉的代码两种情况是相同的。区别:
setContentView()一旦调用,layout就会立刻显示UI;而inflate只会把Layout形成一个以view类实现成的对象,有需要时再用setContentView(view)显示出来。一般在activity中通过setContentView()将界面显示出来,但是如果在非activity中如何对控件布局设置操作了,这就需要LayoutInflater动态加载。publicViewinflate(intResourece,ViewGrouproot)
作用:填充一个新的视图层次结构从指定的XML资源文件中
reSource:View的layout的ID
root:生成的层次结构的根视图
return填充的层次结构的根视图。如果参数root提供了,那么root就是根视图;否则填充的XML文件的根就是根视图。其余几个重载的inflate函数类似。
?
LayoutInflaterinflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); Viewlayout=inflater.inflate(R.layout.main, null ); LayoutInflaterinflater=LayoutInflater.from(context);(该方法实质就是第一种方法,可参考源代码) Viewlayout=inflater.inflate(R.layout.main, null ); LayoutInflaterinflater=getLayoutInflater();(在Activity中可以使用,实际上是View子类下window的一个函数) Viewlayout=inflater.inflate(R.layout.main, null ); |
?
public class MyInflate extends Activity{ private TextViewtv; public void OnCreate(BundlesavedInstanceState){ super .onCreate(savedInstanceState); //setContentView(R.layout.main); //tv=(TextView)findViewById(R.id.tv); LayoutInflaterinflate=LayoutInflater.from( this ); Viewview=inflate.inflate(R.layout.main, null ); setContentView(view); } } |
setContentView()一旦调用,layout就会立刻显示UI;而inflate只会把Layout形成一个以view类实现成的对象,有需要时再用setContentView(view)显示出来。一般在activity中通过setContentView()将界面显示出来,但是如果在非activity中如何对控件布局设置操作了,这就需要LayoutInflater动态加载。publicViewinflate(intResourece,ViewGrouproot)
作用:填充一个新的视图层次结构从指定的XML资源文件中
reSource:View的layout的ID
root:生成的层次结构的根视图
return填充的层次结构的根视图。如果参数root提供了,那么root就是根视图;否则填充的XML文件的根就是根视图。其余几个重载的inflate函数类似。
相关文章推荐
- LayoutInflater的作用和用法(转)
- android--LayoutInflater作用及用法
- Android_LayoutInflater的作用与用法详解
- 安卓笔记:LayoutInflater.inflate的用法
- LayoutInflater.from(this)的用法
- android LayoutInflater.inflate()的参数及其用法
- Android 中 LayoutInflater 的用法小结
- LayoutInflater和inflate()方法的用法 .
- LayoutInflater作用是将layout的xml布局文件实例化为View类对象。
- LayoutInflater 用法总结
- LayoutInflater的用法
- android LayoutInflater用法和源码解析
- LayoutInflater的inflate函数用法详解
- LayoutInflater作用及使用
- getViewById和getLayoutInflater().inflate得用法
- LayoutInflater作用及使用
- LayoutInflater作用及使用--自定义EditText,自带清除内容按钮
- LayoutInflater的inflate函数用法详解
- LayoutInflater的inflate函数用法详解,layoutinflater用法
- LayoutInflater的inflate函数用法详解