setContentView 流程分析

xml加载流程

在这里插入图片描述

继承Activity的流程

在这里插入图片描述

// PhoneWindow的创建---》哪些地方会创建
1.Activity  
2.Dialog
3.PopupWindow
4.Toast

ActivityThread.performLaunchActivity
-->activity.attach
	--> new PhoneWindow()
-->mInstrumentation.callActivityOnCreate
--getWindow().setContentView(layoutResID); PhoneWindow.setContentView --- 主要目的 创建 DecorView拿到Content
	--installDecor(); // 创建 DecorView 拿到 mContentParent
		--》 mDecor = generateDecor(-1);
			--new DecorView()
				--setWindow(PhoneWindow);
		--》 mContentParent = generateLayout(mDecor);
			--》getLocalFeatures
				--R.layout.screen_simple  DecorView下的LinearLayout
			 //   layoutResource =  R.layout.screen_simple 为例 R.layout.screen_simple 在我们getLocalFeatures拿到窗口属性时会设置,比如NO_TITLE,AS新建项目创建Activity选择Activity类型时
			--》 mDecor.onResourcesLoaded(mLayoutInflater, layoutResource);R.layout.screen_simple添加到 DecorViewFrameLayout--》inflater.inflate(layoutResource, null);
					--addView(layoutResource , 0, new ViewGroup.LayoutParams(MATCH_PARENT, MATCH_PARENT)); 
			--ViewGroup contentParent = (ViewGroup)findViewById(ID_ANDROID_CONTENT); contentParent 就是R.layout.screen_simple 里的FrameLayout @android:id/content

	--》 mLayoutInflater.inflate(layoutResID, mContentParent); //  R.layout.activity_main 渲染到 mContentParent

在这里插入图片描述

// 继承 AppCompatActivity 的流程
AppCompatDelegate.setContentView 
-->getDelegate()
	-->AppCompatDelegateImpl.onCreate
		--ensureWindow();	 AppCompat install itself into the Window																				
--> ensureSubDecor();
	--> mSubDecor = createSubDecor();
		--> ensureWindow(); // 从Activity 那PhoneWindow AppCompatDelegateImpl例onCreate已经创建
		--> mWindow.getDecorView();
			--> installDecor(); // mContentParent
				--> //看继承 Activity的流程
               			-->  subDecor = (ViewGroup) inflater.inflate(R.layout.abc_screen_simple, null);
				--> final ViewGroup windowContentView = (ViewGroup) mWindow.findViewById(android.R.id.content);
					-->while (windowContentView.getChildCount() > 0) // R.layout.screen_simple 里面的 content  ---》 把 content 的 View移动到 R.id.action_bar_activity_content,避免app代码已经把avtivity_main里的view添加到windowContentView(android.R.id.content)里了
					--> windowContentView.setId(View.NO_ID); // 将原始的 content id 置为 NO_ID
      					 --> contentView.setId(android.R.id.content); // subDecerView R.id.action_bar_activity_content --> 置为 content
        					--> mWindow.setContentView(subDecor); /
						--》mContentParent.addView(view, params); mContentParent就是R.layout.activity_main
--> ViewGroup contentParent = mSubDecor.findViewById(android.R.id.content);
--> LayoutInflater.from(mContext).inflate(resId, contentParent);
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值