LayoutInflater作用是将layout的xml布局文件实例化为View类对象。
2013-09-16 19:34
666 查看
获取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 ); |
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); } } |
相关文章推荐
- LayoutInflater作用是将layout的xml布局文件实例化为View类对象。
- LayoutInflater将layout的xml布局文件实例化为View类对象
- Android中将xml布局文件转化为View树的过程分析(下)-- LayoutInflater源码分析
- LayoutInflater这个类还是非常有用的,它的作用类似于findViewById()。 但他是找XML文件并实例化
- LayoutInflater和inflate()方法的用法 (自定义View中加载xml布局)
- 从LayoutInflater分析XML布局解析成View的树形结构的过程
- 自定义View实例——下雨天画面(通过布局文件.xml中的自定义属性来设值)
- 从LayoutInflater分析XML布局解析成View的树形结构的过程
- LayoutInflater和inflate()方法的用法 (自定义View中加载xml布局)
- Android 源码解析(01) xml布局文件解析——LayoutInflater源码解析
- Android 源码解析(01) xml布局文件解析——LayoutInflater源码解析
- 从LayoutInflater分析XML布局解析成View的树形结构的过程
- android xml布局文件中tools:layout的作用
- 从LayoutInflater分析XML布局解析成View的树形结构的过程
- LayoutInflater-将XML文件转为View
- android xml布局文件中tools:layout的作用
- Android布局文件.xml中的自定义属性(结合封装的自定义View)
- you must supply a layout_height attribute——android开发之xml布局文件
- 【Android】Android Layout Binder——根据layout布局文件自动生成findViewById的java代码的神器
- 使用LayoutInflater装载布局或者setContentView装载布局的时候出现OOM(内存溢出)的解决办法