当前位置: 首页 > news >正文

Android setContentView()源码分析

文章目录

  • Android setContentView()源码分析
    • 前提
    • setContentView() 源码分析
    • 总结

Android setContentView()源码分析

前提

Activity 的生命周期与 ActivityThread 相关,调用 startActivity() 时,会调用 ActivityThread#performLaunchActivity(),接着调用 Activity#attach() 并在其中创建 PhoneWindow。

Activity 持有 PhoneWindow 对象,PhoneWindow 持有 DecorView。

private Activity performLaunchActivity(ActivityClientRecord r, Intent customIntent) {activity.attach(appContext, this, getInstrumentation(), r.token,r.ident, app, r.intent, r.activityInfo, title, r.parent,r.embeddedID, r.lastNonConfigurationInstances, config,r.referrer, r.voiceInteractor, window, r.activityConfigCallback,r.assistToken, r.shareableActivityToken);return activity;
}
public class Activity {private Window mWindow;final void attach(Context context, ActivityThread aThread,Instrumentation instr, IBinder token, int ident,Application application, Intent intent, ActivityInfo info,CharSequence title, Activity parent, String id,NonConfigurationInstances lastNonConfigurationInstances,Configuration config, String referrer, IVoiceInteractor voiceInteractor,Window window, ActivityConfigCallback activityConfigCallback, IBinder assistToken,IBinder shareableActivityToken) {// 创建PhoneWindowmWindow = new PhoneWindow(this, window, activityConfigCallback);}
}
public class PhoneWindow extends Window {private DecorView mDecor;ViewGroup mContentParent;
}

setContentView() 源码分析

// Activity#setContentView()
public void setContentView()(@LayoutRes int layoutResID) {// 调用PhoneWindow#setContentView()getWindow().setContentView(layoutResID);initWindowDecorActionBar();
}public Window getWindow() {return mWindow;
}
// PhoneWindow#setContentView()
// 核心代码:
public void setContentView()(int layoutResID) {if (mContentParent == null) {// 初始化DecorViewinstallDecor();} else if (!hasFeature(FEATURE_CONTENT_TRANSITIONS)) {mContentParent.removeAllViews();}// 将布局添加到ContentView中mLayoutInflater.inflate(layoutResID, mContentParent);
}
// PhoneWindow#installDecor()
private void installDecor() {mForceDecorInstall = false;if (mDecor == null) {// 创建DecorViewmDecor = generateDecor(-1);} else {mDecor.setWindow(this);}if (mContentParent == null) {// 获取ContentViewmContentParent = generateLayout(mDecor);}
}
// PhoneWindow#generateDecor()
protected DecorView generateDecor(int featureId) {Context context;if (mUseDecorContext) {Context applicationContext = getContext().getApplicationContext();if (applicationContext == null) {context = getContext();} else {context = new DecorContext(applicationContext, this);if (mTheme != -1) {context.setTheme(mTheme);}}} else {context = getContext();}// 创建DecorView,DecorView继承自FrameLayoutreturn new DecorView(context, featureId, this, getAttributes());
}
// PhoneWindow#generateLayout()
// 核心代码:
protected ViewGroup generateLayout(DecorView decor) {// 布局idint layoutResource;int features = getLocalFeatures();// 省略layoutResource赋值流程,根据主题赋值// ...// 加载layoutResource生成View,并加载到DecorView中mDecor.onResourcesLoaded(mLayoutInflater, layoutResource);// 通过DecorView获取id为ID_ANDROID_CONTENT的ViewViewGroup contentParent = (ViewGroup)findViewById(ID_ANDROID_CONTENT);    return contentParent;
}

总结

执行流程:

  • Activity#setContentView()
  • PhoneWindow#setContentView()
  • PhoneWondow#installDecor()
    • PhoneWindow#generateDecor() 创建DecorView
    • PhoneWindow#generateLayout() 加载layoutResource并获取ContentView
  • 将自定义布局添加到ContentView中

相关文章:

  • [学习]RTKLib详解:rtkcmn.c与rtkpos.c
  • Java实现堆排序算法
  • 【省电级子印章系统建设方案】
  • 一款独立于游戏外的键盘源按键辅助工具他来了
  • 2025年AI视觉革命:Dual-Stream Diffusion如何让PS下岗?
  • 交换机 MUX VLAN
  • 雨云游戏云MCSM面板服使用教程我的世界Forge服务端开服教程
  • 通话篇:FreeBuds 6的正确打开方式!
  • 优化04-选择率和直方图
  • 小红书怎么看自己ip地址?小红书更改ip地址教学
  • 地级市-机器人、人工智能等未来产业水平(2009-2023年)-社科数据
  • 电子电器架构 --- 网关ECU中采用多CPU解决方案来实现网关功能
  • postgresql 创建、移出数据保留策略
  • 基于 Flask的深度学习模型部署服务端详解
  • 关于Java多态简单讲解
  • 社交距离调节下的情绪神经反应:基于360°视频的fMRI证据
  • 浏览器开发者工具Network面板中复制请求的相关信息(结合PostMan)
  • Redis设置永久密码
  • 指针的应用
  • DIC技术在焊接试样循环加载疲劳试验中的应用探究
  • 上海乐高乐园度假区将于7月5日开园
  • 国际上首次,地月空间卫星激光测距试验在白天成功实施
  • 一周人物|何子彦任职光州双年展,陈意心讲述五原路往事
  • 马上评|比起奇葩论文,更可怕的是“水刊”灰产
  • 许昌市场监管部门对胖东来玉石开展日常检查:平均毛利率不超20%
  • 两个灵魂,一支画笔,意大利艺术伴侣的上海灵感之旅