«

Android中setContentView、addContentView、inflate、LayoutInflater和findViewById()区别

时间:2024-3-2 18:07     作者:韩俊     分类: Android


Android中setContentView、addContentView、inflate、LayoutInflater和findViewById()区别:

1. setContentView和addContentView的区别:
都是加载布局文件并显示,为布局文件分配内存。
(1).setContentView()会导致先前添加的组件被移除。
(2).addContentView() 不会移除先前添加的UI组件。
2. setContentView和inflate的区别:
(1).setContentView()一旦调用,layout就会立刻显示UI;inflate只是把Layout转换成一个View对象,若需要显示的话用setContentView(view)显示出来。
(2).一般在activity中通过setContentView()将界面显示出来,但是如果在非activity中如何对控件布局设置操作了,这需LayoutInflater动态加载。
3. LayoutInflater和findViewById()区别:
(1). LayoutInflater.inflate()将Layout文件转换为View对象,专门供Layout使用的Inflater,不能通过findViewById()。
(2).LayoutInflater是用来找layout下xml布局文件,并且实例化。findViewById()是找layout下xml布局文件下的具体widget控件(如:Button,TextView等)。

标签: android

热门推荐