1.引言

最近准备重新学习下Android,加深理解,快速形成自己的知识结构体系。最先学习的就算View部分,从自定义View到Activty层次结构,到layout加载过程。等等都会看一遍,在此记录下Layout的加载过程

2.正题

2.1 Activity的流程加载

Activity类中setContentView 追踪(善于用bookMark)

 public void setContentView(@LayoutRes int layoutResID) {
getWindow().setContentView(layoutResID);
initWindowDecorActionBar();
}

追踪代码,PhoneWindow的setContentView方法:

 
image.png
mLayoutInflater.inflate(layoutResId,mContentParent)

由代码片段可以看出来:layoutResId的父布局就是mContentParent。那么mContentParent究竟是什么呢?

我们知道DecorView 包括TitleView +ContentView.

 
image.png

追踪代码:installDescor():

 
image.png

generateLayout()方法:

protected ViewGroup generateLayout(DecorView decor) {
// Apply data from current theme.
// 获得窗体的 style 样式
TypedArray a = getWindowStyle(); // 省略大量无关代码 // Inflate the window decor.
int layoutResource;
int features = getLocalFeatures(); //填充带有 style 和 feature 属性的 layoutResource (是一个layout id) View in = mLayoutInflater.inflate(layoutResource, null); // 插入的顶层布局 DecorView 中 decor.addView(in, new ViewGroup.LayoutParams(MATCH_PARENT, MATCH_PARENT));
mContentRoot = (ViewGroup) in; // 找到我们XML文件的父布局 contentParent ViewGroup contentParent = (ViewGroup)findViewById(ID_ANDROID_CONTENT);
if (contentParent == null) {
throw new RuntimeException("Window couldn't find content container view");
}
// 省略无关代码
mDecor.finishChanging();
// 返回 contentParent 并赋值给成员变量 mContentParent
return contentParent;
}

给DecorView添加一个名字为mContentRoot的view。并且mContentRoot里面包含了一个id为ID_ANDROID_CONTENT的 mContentParent.

得到了mContentParent之后,在根据mLayoutInflater.inflate(layoutResId,mContentParent) 就将Layout布局加载进了DecorView,然后这个视图才能被我们看到。

验证:

 
image.png

说明了mContentParent是FramLayout

3.AppCompatActivity 加载View得过程

  @Override
public void setContentView(@LayoutRes int layoutResID) {
getDelegate().setContentView(layoutResID);
}

getDelegate():

    @NonNull
public AppCompatDelegate getDelegate() {
if (mDelegate == null) {
mDelegate = AppCompatDelegate.create(this, this);
}
return mDelegate;
}

跟踪create()方法,

  public static AppCompatDelegate create(Activity activity, AppCompatCallback callback) {
return create(activity, activity.getWindow(), callback);
}

activity.getWindow() 由上面可知,得到的肯定是PhoneWindow。问题来了,activity这个时候真的能得到一个非null的PhoneWindow吗?。发现在activity调用attach方法的时候就会初始化PhoneWindow:

 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) {
attachBaseContext(context); mFragments.attachHost(null /*parent*/); mWindow = new PhoneWindow(this, window, activityConfigCallback);
mWindow.setWindowControllerCallback(this);
......
}

Activity.attach是在performLaunchActivity中本调用,也就是在ActivityThread中被调用。所以在Activity中任何时候getWindow(),都能得到PhoneWindow。

继续跟踪代码:
create方法调用的是:

 private static AppCompatDelegate create(Context context, Window window,
AppCompatCallback callback) {
if (Build.VERSION.SDK_INT >= 24) {
return new AppCompatDelegateImplN(context, window, callback);
} else if (Build.VERSION.SDK_INT >= 23) {
return new AppCompatDelegateImplV23(context, window, callback);
} else if (Build.VERSION.SDK_INT >= 14) {
return new AppCompatDelegateImplV14(context, window, callback);
} else if (Build.VERSION.SDK_INT >= 11) {
return new AppCompatDelegateImplV11(context, window, callback);
} else {
return new AppCompatDelegateImplV9(context, window, callback);
}
}

setContentView 是在AppCompatDelegateImplV9类中被重写,也就是说最终是调用AppCompatDelegateImplV9.setContentView();

  @Override
public void setContentView(int resId) {
ensureSubDecor();
ViewGroup contentParent = (ViewGroup) mSubDecor.findViewById(android.R.id.content);
contentParent.removeAllViews();
LayoutInflater.from(mContext).inflate(resId, contentParent);
mOriginalWindowCallback.onContentChanged();
}
  ViewGroup contentParent = (ViewGroup) mSubDecor.findViewById(android.R.id.content);

很容易让我们想到mSubDecor 是不是等同于上文中的mContentRoot 呢?

看看ensureSubDecor怎么写的:

 if (mOverlayActionMode) {
subDecor = (ViewGroup) inflater.inflate(
R.layout.abc_screen_simple_overlay_action_mode, null);
} else {
subDecor = (ViewGroup) inflater.inflate(R.layout.abc_screen_simple, null);
}

subDecor 是系统提供的ViewGroup 里面有一个android.R.id.content布局,和mContentRoot 作用一模一样。

基本上分析到此结束,也是比较简单。

 

Android View之布局加载流程的更多相关文章

  1. android源码解析(十七)-->Activity布局加载流程

    版权声明:本文为博主原创文章,未经博主允许不得转载. 好吧,终于要开始讲讲Activity的布局加载流程了,大家都知道在Android体系中Activity扮演了一个界面展示的角色,这也是它与andr ...

  2. Android View的加载流程

    什么是Activity? Activity是 用户操作的可视化界面:它为用户提供了一个放置视图和交互操作的窗口.采用setContentView的方法提供.因此,可以理解Activity.Window ...

  3. Android 8.1 SystemUI虚拟导航键加载流程解析

    需求 基于MTK 8.1平台定制导航栏部分,在左边增加音量减,右边增加音量加 思路 需求开始做之前,一定要研读SystemUI Navigation模块的代码流程!!!不要直接去网上copy别人改的需 ...

  4. 8. Android加载流程(打包与启动)

    移动安全的学习离不开对Android加载流程的分析,包括Android虚拟机,Android打包,启动流程等... 这篇文章  就对Android的一些基本加载进行学习. Android虚拟机 And ...

  5. Android必学-异步加载+Android自定义View源码【申明:来源于网络】

    Android必学-异步加载+Android自定义View源码[申明:来源于网络] 异步加载地址:http://download.csdn.net/detail/u013792369/8867609 ...

  6. Android 自定义View修炼-自定义加载进度动画XCLoadingImageView

    一.概述 本自定义View,是加载进度动画的自定义View,继承于ImageView来实现,主要实现蒙层加载进度的加载进度效果. 支持水平左右加载和垂直上下加载四个方向,同时也支持自定义蒙层进度颜色. ...

  7. Android 如果布局中有ScrollView和Fragment或者带有滚动条的布局进行嵌套,布局加载完成页面无法定位到顶部的情况

    页面无法定位到顶部: 1.ScrollView中嵌套Fragment(Fragment中可能有想ScrollView,ListView带有滚动条的控件). 2.ScrollView嵌套ScrollVi ...

  8. FrameWork内核解析之布局加载与资源系统(三)

    阿里P7Android高级架构进阶视频免费学习请点击:https://space.bilibili.com/474380680本篇文章将继续从以下两个内容来介绍布局加载与资源系统: [ LayoutM ...

  9. java攻城狮之路(Android篇)--widget_webview_metadata_popupwindow_tabhost_分页加载数据_菜单

    一.widget:桌面小控件1 写一个类extends AppWidgetProvider 2 在清单文件件中注册: <receiver android:name=".ExampleA ...

随机推荐

  1. 自然语言处理哪家强?【36kr】

    语音交互事关未来,国内外已经不少公司在抢蛋糕了,大公司收购.投资.合作不断,就可见一斑.目前,基本上所有的巨头都有涉足. 苹果收购 Siri.Novauris,组建基于神经网络算法的语音识别团队 20 ...

  2. SAN和虚拟化,NUMA等

    Virtual Networking Best Practices Following are the vSphere networking best practices: •Separate vir ...

  3. 关于LINUX在中断(硬软)中不能睡眠的真正原因

    摘自http://bbs.chinaunix.net/thread-2115820-1-1.html 4楼的回答 先把中断处理流程给出来 1.进入中断处理程序--->2.保存关键上下文----& ...

  4. HttpLuaModule——翻译(一)

    最近经常使用春哥和小哲老师写的NGINX-LUA,非常苦于没有中文文档,特别是向我这种英文水平实在有限的同学,所以将遇到的模块记录下来,供以后参考!原文:http://wiki.nginx.org/H ...

  5. tomcat6url请求400错误(%2F与%5C)

    近期几天,开发接口时.tomcat报了400错误,查了下原因. 错误原因:url中參数部分包括/,默认tomcat是不支持url參数包括: /(%2F),\(%5C). 解析方法:能够通过加入配置Do ...

  6. oracle 清空表数据的2种方式及速度比较

            1.情景展示 现在,需要清空该表数据 2.实现方式 为了比较删除速度,对该表进行复制 确认复制的表和原来的表数据是否一致 方式一:使用truncate table实现 方式二:使用de ...

  7. 〖Linux〗使用sed命令修改小端(little endian)存储的数据

    #!/bin/bash - #=============================================================================== # # F ...

  8. python之模块 os

    # -*- coding: utf-8 -*- #python 27 #xiaodeng #python之模块 os import os ''' FUNCTIONS abort(...)#暂不了解 a ...

  9. Windows开发进阶之VC++中如何实现对话框的界面重绘

    技术:Windows 系统+Visual studio 2008   概述 应用程序界面是用户与应用程序之间的交互的桥梁和媒介,用户界面是应用程序中最重要的组成部分,也是最为直观的视觉体现.对用户而言 ...

  10. Oracle Tuxedo的配置文件配置详解

    # (c) 2003 BEA Systems, Inc. All Rights Reserved. #ident "@(#) samples/atmi/simpapp/ubbsimple $ ...