Android UI 详解之LayoutInflater和setContentView
来源:互联网 发布:计算机三级 数据库 编辑:程序博客网 时间:2024/04/28 08:34
Android UI 详解之LayoutInflater
1、LayoutInflater
LayoutInflater作用是将layout的xml布局文件实例化为View类对象。
2、LayoutInflater实例化方法有以下3种
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();(在Activity中可以使用,实际上是View子类下window的一个函数)
View layout = inflater.inflate(R.layout.main,
null
);
3、下面看一个例子
public class MyInflate extends Activity{ private TextView tv; public void OnCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); //setContentView(R.layout.main); //tv = (TextView) findViewById(R.id.tv); LayoutInflater inflate = LayoutInflater.from(this); View view = inflate.inflate(R.layout.main,null); setContentView(view); }}
所以我们能了解setContentView和inflate的区别
setContentView可以传递一个resourceId也可传递View对象,如果传递resourceId,也是先实例一个View并显示,
如果穿的view就直接显示,而inflate就只是加载实例一个view对象,并不显示,所以如果在非activity中控制布局设置操作就只能用inflate。
4、android上还有一个与Inflate()类似功能的方法叫findViewById(),二者有时均可使用,但也有区别
inflate是用来找layout下布局文件,并实例化;而findViewById是找布局文件下的具体widget。
5、详细剖析一下setContentView
当我们运行程序的时候,有一个setContentView()方法,Activity其实不是显示视图(直观上感觉是它),实际上Activity调用了PhoneWindow的setContentView()方法,然后加载视图,将视图放到这个Window上,而Activity其实构造的时候初始化的是Window(PhoneWindow),Activity其实是个控制单元,即可视的人机交互界面。
打个比喻:
Activity是一个工人,它来控制Window;Window是一面显示屏,用来显示信息;View就是要显示在显示屏上的信息,这些View都是层层重叠在一起(通过infalte()和addView())放到Window显示屏上的。而LayoutInfalter就是用来生成View的一个工具,XML布局文件就是用来生成View的原料
getWindow().setContentView(layoutResID);
initActionBar();
}
public void setContentView(int layoutResID) {
if (mContentParent == null) {
installDecor();
} else {
mContentParent.removeAllViews();
}
mLayoutInflater.inflate(layoutResID, mContentParent);
final Callback cb = getCallback();
if (cb != null && !isDestroyed()) {
cb.onContentChanged();
}
}
- Android UI 详解之LayoutInflater和setContentView
- 【Android】 LayoutInflater和SetContentView详解
- Android之setContentView和LayoutInflater
- Android之setContentView和LayoutInflater
- Android之setContentView和LayoutInflater
- Android之setContentView和LayoutInflater
- Android Activity的UI绘制流程之setContentView方法详解
- Android setContentView、LayoutInflater、findViewById
- [Android基础知识] 之二十二 UI设计之 LayoutInflater详解
- Android中setContentView、addContentView、inflate、LayoutInflater和findViewById()区别
- android开发中使用setContentView和LayoutInflater的点点滴滴
- Android 之LayoutInflater详解
- Android之LayoutInflater详解
- android setContentView() 、LayoutInflater的区别
- setContentView和LayoutInflater相关总结
- android之LayoutInflater详解_什么是LayoutInflater
- Android view绘制之setContentView详解
- setContentView()与LayoutInflater.from()的区别-android
- Dtrace 资源库 URL 大全
- 将matlab原始代码的ROC曲线张贴出来供大家学习
- [水题][第一阶段-回归水题][HDOJ-2018]母牛的故事
- 黑马程序员_java GUI 三大布局使用
- 民警酒桌上赌玩 英雄联盟 少年挑战警察被
- Android UI 详解之LayoutInflater和setContentView
- 【FacebookSDK学习笔记】Facebook官方Demo例子简单分析
- Oracle逻辑存储结构---(一) 数据块
- 函数指针
- 排序高级之交换排序_奇偶排序
- Ubuntu系统配置你的DHCP服务器
- Android程序Crash时的异常上报
- linux内核分析笔记
- 贪吃蛇游戏代码