1.操作对象
- inflate()方法是用来将res/layout/下的xml布局文件实例化,操作对象是XML文件返回ViewGroup对象.
- findViewById()是找已被实例化为View对象的xml布局文件下的具体控件(如Button、TextView等),操作对象是一个ViewGroup或者是Activity,返回一个View对象.
2.功能
- 对于一个没有被载入或者想要动态载入的界面,都需要使用LayoutInflater.inflate()来载入
- 对于一个已经载入的界面,就可以使用Activity.findViewById()方法来获得其中的界面元素
3.调用方法
- 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();
View layout = inflater.inflate(R.layout.main, null);
- findViewById
//假设已经通过LayoutInflater加载了一个ViewGroup vp
View view = vp.findViewById(R.id.res_id);
//通过强制类型转换转换成你在XML里面定义的对象比如在XML里面定义了一个Button,即可获取到这个Button的对象
Button bt = (Button)view;
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。