LayoutInflater和inflate()方法的用法 (自定义View中加载xml布局)

实现LayoutInflater的实例化共有3种方法:

1. 通过SystemService获得

LayoutInflater inflater = (LayoutInflater)context.getSystemServices(Context.LAYOUT_INFLATER_SERVICES);

View view = inflater.inflate(R.layout.main, null);

2. 从给定的context中获得

LayoutInflater inflater = LayoutInflater.from(context);

View view = inflater.inflate(R.layout.mian, null);

3.

LayoutInflater inflater = getLayoutInflater();(在Activity中可以使用,实际上是View子类下window的一个函数)

Viewlayout = inflater.inflate(R.layout.main, null);

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

推荐阅读更多精彩内容