android学习关于LayoutInflater的使用
2013-06-04 22:22
405 查看
之前在一些应用中看到很多次使用LayoutInflater这个类,感觉这个类还是很有作用,于是查看android开发文档,有如下对真个类的Overview:
Instantiates a layout XML file into its corresponding
当我们需要在代码中载入一个布局,或者是要动态的载入一个布局的时候,我们一般会用到LayoutInflater这个类,换言之,我们在通常在onCreate函数中通过setContentView中载入xml布局。通过LayoutInflater这个类我们便可以在代码的别的地方动态载入我们定义的xml布局(Layout文件下的)
首先要获得layout的实例,有三种形式:
1)LayoutInflater inflater = getLayoutInflater();
2)LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
3)LayoutInflater inflater = LayoutInflater.from(context);
然后可以通过inflate将一个布局填充进来,如:
View layout = inflater.inflate(R.layout.dialog, null);
其中在layout文件下自己定义了一个dialog.xml文件,对于inflate函数的的使用解释,在android开发文档上这样解释:Inflate a new view hierarchy from the specified xml node。
Instantiates a layout XML file into its corresponding
Viewobjects. It is never used directly. Instead, use
getLayoutInflater()or
getSystemService(String)to retrieve a standard LayoutInflater instance that is already hooked up to the current context and correctly configured for the device you are running on. For example:
LayoutInflater inflater =(LayoutInflater)context.getSystemService (Context.LAYOUT_INFLATER_SERVICE);To create a new LayoutInflater with an additional
LayoutInflater.Factoryfor your own views, you can use
cloneInContext(Context)to clone an existing ViewFactory, and then call
setFactory(LayoutInflater.Factory)on it to include your Factory.For performance reasons, view inflation relies heavily on pre-processing of XML files that is done at build time. Therefore, it is not currently possible to use LayoutInflater with an XmlPullParser over a plain XML file at runtime; it only works with an XmlPullParser returned from a compiled resource (R.something file.)然后结合一下网上的资源,做个小总结:
当我们需要在代码中载入一个布局,或者是要动态的载入一个布局的时候,我们一般会用到LayoutInflater这个类,换言之,我们在通常在onCreate函数中通过setContentView中载入xml布局。通过LayoutInflater这个类我们便可以在代码的别的地方动态载入我们定义的xml布局(Layout文件下的)
首先要获得layout的实例,有三种形式:
1)LayoutInflater inflater = getLayoutInflater();
2)LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
3)LayoutInflater inflater = LayoutInflater.from(context);
然后可以通过inflate将一个布局填充进来,如:
View layout = inflater.inflate(R.layout.dialog, null);
其中在layout文件下自己定义了一个dialog.xml文件,对于inflate函数的的使用解释,在android开发文档上这样解释:Inflate a new view hierarchy from the specified xml node。
相关文章推荐
- Android学习之LayoutInflater类和inflate()方法的使用
- Android学习之LayoutInflater类的使用
- android 入门学习笔记 LayoutInflater的使用
- 【android】关于使用ViewPager和LayoutInflater的出现问题
- android学习—— LayoutInflater的使用
- android控件学习二:使用LayoutInflater动态加载布局和操作控件
- android关于TabLayout+ViewPager结合使用的小Demo
- android中LayoutInflater的使用
- android LayoutInflater使用
- Android LayoutInflater学习之 原理分析
- android中LayoutInflater的3种使用以及getSystemService的API .
- Android高手进阶教程(五)之----Android 中LayoutInflater的使用!
- Android 中LayoutInflater的使用!
- Android 中LayoutInflater的使用
- Android中LayoutInflater的使用
- Android高手进阶教程(五)之----Android 中LayoutInflater的使用!
- Android LayoutInflater源码分析及使用(一)
- Android高手进阶教程(五)之----Android 中LayoutInflater的使用!
- android LayoutInflater学习
- 黑马程序员————Android中LayoutInflater布局解析的使用