Android LayoutInflater

LayoutInflater是用来找res/layout/下的xml布局文件,并且实例化;类似findViewById()是找xml布局文件下的具体widget控件(如Button、TextView等),并且实例化。

获得 LayoutInflater 实例的三种方式

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

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

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

研究源码可以看到 getLayoutInflater() 中调用了 LayoutInflater.from(context), 而LayoutInflater.from(context) 中又调用了(LayoutInflater)context.getSystemService 所以可以知道,三种调用方式本质是没有区别的。

参考:

https://blog.csdn.net/u012702547/article/details/52628453

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容