设为首页 加入收藏

TOP

Android开发心得:LayoutInflater及inflate方法
2014-11-23 18:54:46 来源: 作者: 【 】 浏览:38
Tags:Android 开发 心得 LayoutInflater inflate 方法

引言:


public void OnCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);


这个是Activity创建时执行的方法,其中最后一句大家应该都见过,这个加载布局的最简单的方法,但是如果,你需要动态加载布局,那就需要使用其他办法。


那就是LayoutInfater的inflate方法。


一。获得 LayoutInflater 实例的三种方式


1.LayoutInflater inflater = getLayoutInflater(); //调用Activity的getLayoutInflater()


2.LayoutInflater localinflater =(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);


3. LayoutInflater inflater = LayoutInflater.from(context);


二。inflate方法


public View inflate(int Resourece,ViewGroup root)


作用:填充一个新的视图层次结构从指定的XML资源文件中


reSource:View的layout的ID


root: 生成的层次结构的根视图


return 填充的层次结构的根视图


如果参数root提供了,那么root就是根视图;否则填充的XML文件的根就是根视图。其余几个重载的inflate函数类似。


结束语:


这段代码和之前说的那最后一句是一个意思:


LayoutInflater inflate = LayoutInflater.from(this);
View view = inflate.inflate(R.layout.main,null);
setContentView(view);


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Android高级教程:使用NDK把彩图.. 下一篇OpenCV4Android--一点关于图像的..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: