Activity中的setContentView(R.layout.xxx)源码分析
点进去会看到下图:

其中getWindow()是获取到Window类的唯一子类PhoneWindow的对象

找到PhoneWindow的setContentView()方法点进去:
1 @Override
2 public void setContentView(int layoutResID) {
3 // Note: FEATURE_CONTENT_TRANSITIONS may be set in the process of installing the window
4 // decor, when theme attributes and the like are crystalized. Do not check the feature
5 // before this happens.
6 if (mContentParent == null) {
7 installDecor();
8 } else if (!hasFeature(FEATURE_CONTENT_TRANSITIONS)) {
9 mContentParent.removeAllViews();
10 }
11
12 if (hasFeature(FEATURE_CONTENT_TRANSITIONS)) {
13 final Scene newScene = Scene.getSceneForLayout(mContentParent, layoutResID,
14 getContext());
15 transitionTo(newScene);
16 } else {
17 mLayoutInflater.inflate(layoutResID, mContentParent);
18 }
19 mContentParent.requestApplyInsets();
20 final Callback cb = getCallback();
21 if (cb != null && !isDestroyed()) {
22 cb.onContentChanged();
23 }
24 }
从上述代码中可以看出:把我们的layoutResID放在了mContentParent里面
在类中可以看出mContentParent是一个ViewGroup
点进去查看:installDecor()方法


继续跟进:generateLayout()方法
protected ViewGroup generateLayout(DecorView decor) {
// Apply data from current theme.
TypedArray a = getWindowStyle();
if (false) {
System.out.println("From style:");
String s = "Attrs:";
for (int i = 0; i < R.styleable.Window.length; i++) {
s = s + " " + Integer.toHexString(R.styleable.Window[i

]) + "="
+ a.getString(i);
}
System.out.println(s);
}
//根据style属性做一些列的判断...
//在做一些列的判断得到layoutResource
layoutResource = R.layout.screen_swipe_dismiss; //这里用R.layout.screen_simple来分析
mDecor.startChanging();
View in = mLayoutInflater.inflate(layoutResource, null);
//把layoutResource放进decor中去
decor.addView(in, new ViewGroup.LayoutParams(MATCH_PARENT, MATCH_PARENT));
mContentRoot = (ViewGroup) in;
ViewGroup contentParent = (ViewGroup)findViewById(ID_ANDROID_CONTENT);
if (contentParent == null) {
throw new RuntimeException("Window couldn't find content container view");
}
//......
mDecor.finishChanging();
//返回创建的这个布局
return contentParent;
}
//贴出这个布局文件

if (mDecor == null) {
mDecor = generateDecor();
mDecor.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
mDecor.setIsRootNamespace(true);
if (!mInvalidatePanelMenuPosted && mInvalidatePanelMenuFeatures != 0) {
mDecor.postOnAnimation(mInvalidatePanelMenuRunnable);
}
}
protected DecorView generateDecor() {
return new DecorView(getContext(), -1);
}
mDecor 就是DecorView(FragmentLayout)的一个实例
所以最终的图形就是

Activity中的setContentView(R.layout.xxx)源码分析的更多相关文章
- java中的==、equals()、hashCode()源码分析(转载)
在java编程或者面试中经常会遇到 == .equals()的比较.自己看了看源码,结合实际的编程总结一下. 1. == java中的==是比较两个对象在JVM中的地址.比较好理解.看下面的代码: ...
- Vue3中的响应式对象Reactive源码分析
Vue3中的响应式对象Reactive源码分析 ReactiveEffect.js 中的 trackEffects函数 及 ReactiveEffect类 在Ref随笔中已经介绍,在本文中不做赘述 本 ...
- Android中Handler的消息处理机制以及源码分析
在实际项目当中,一个很常见的需求场景就是在根据子线程当中的数据去更新ui.我们知道,android中ui是单线程模型的,就是只能在UI线程(也称为主线程)中更新ui.而一些耗时操作,比如数据库,网络请 ...
- 【小家Spring】聊聊Spring中的数据绑定 --- DataBinder本尊(源码分析)
每篇一句 唯有热爱和坚持,才能让你在程序人生中屹立不倒,切忌跟风什么语言或就学什么去~ 相关阅读 [小家Spring]聊聊Spring中的数据绑定 --- 属性访问器PropertyAccessor和 ...
- Java并发包中Semaphore的工作原理、源码分析及使用示例
1. 信号量Semaphore的介绍 我们以一个停车场运作为例来说明信号量的作用.假设停车场只有三个车位,一开始三个车位都是空的.这时如果同时来了三辆车,看门人允许其中它们进入进入,然后放下车拦.以后 ...
- 详解SpringMVC中Controller的方法中参数的工作原理[附带源码分析]
目录 前言 现象 源码分析 HandlerMethodArgumentResolver与HandlerMethodReturnValueHandler接口介绍 HandlerMethodArgumen ...
- 【MVC - 参数原理】详解SpringMVC中Controller的方法中参数的工作原理[附带源码分析]
前言 SpringMVC是目前主流的Web MVC框架之一. 如果有同学对它不熟悉,那么请参考它的入门blog:http://www.cnblogs.com/fangjian0423/p/spring ...
- RocketMQ中PullConsumer的消息拉取源码分析
在PullConsumer中,有关消息的拉取RocketMQ提供了很多API,但总的来说分为两种,同步消息拉取和异步消息拉取 同步消息拉取以同步方式拉取消息都是通过DefaultMQPullConsu ...
- 2、JDK8中的HashMap实现原理及源码分析
本篇提纲.png 本篇所述源码基于JDK1.8.0_121 在写上一篇线性表的文章的时候,笔者看的是Android源码中support24中的Java代码,当时发现这个ArrayList和Linked ...
- JDK1.8中LinkedList的实现原理及源码分析
详见:https://blog.csdn.net/cb_lcl/article/details/81222394 一.概述 LinkedList底层是基于双向链表(双向链表的特点, ...
随机推荐
- java 与 JSON
Java 与 JSON JSON 是不同程序之间传递信息的一种格式.本文描述了 JSON 在 Java 中的应用. 目前 Java 中比较主流的相关解析工具有谷歌提供的 Gson 和阿里提供的 Fas ...
- applicationContext使用注解代替
创建一个类SpringConfig @Configuration//证明这个类是spring的配置文件类 @ComponentScan("com.itheima")//扫描的哪些包 ...
- Ubuntu-18.04.1-live-server-amd64.iso安装全过程
以下向你介绍安装Ubuntu-18.04.1-live-server-amd64.iso版本的全过程,同时要说明的是Ubuntu 18.04.1 server版本只能通过文本的方式安装,即它没有和Ub ...
- webpack配置跨域proxy
首先新建一个项目: 安装vue-cli: npm i -g @vue/cli npm i -g @vue/cli-init 安装webpack: npm install webpack -g vue新 ...
- power shell 删除应用
public static UwpAppInfo SearchUwpAppByName(string appName) { UwpAppInfo app = null; try { string re ...
- [Leetcode 111]二叉树的最短深度 BFS/DFS
题目 给定二叉树,求最短路径包含的节点个数 https://leetcode.com/problems/minimum-depth-of-binary-tree/ Given a binary tre ...
- jooq简单使用
DSLContext首先简单的配置文件 <?xml version="1.0" encoding="UTF-8" standalone="yes ...
- Spring Boot 配置文件加载位置及优先级
内容摘自http://c.biancheng.net/spring_boot/config-order.html 说明如下: /myBoot:表示 JAR 包所在目录,目录名称自定义: /childD ...
- C++ STL的简单应用(vector容器专题)
#include <iostream> #include <string> #include <stdlib.h> #include <vector> ...
- 7 Free Energies: 7.4 Umbrella Sampling Example
7.4 Umbrella Sampling Example计算丙氨酸二肽 Phi/Psi 旋转的 PMF http://ambermd.org/tutorials/advanced/tutori ...