在Activity的生命周期onCreate中。我们一般都习惯性的调用setContentView(int layoutResID)方法,把布局文件载入到页面上来。以下我们就来通过源代码一步步的分析怎么载入的。

在Activity中,调用的是Window的setContentView

public void setContentView(int layoutResID) {

        getWindow().setContentView(layoutResID);

        initWindowDecorActionBar();

    }

我们通过源代码发现Window是一个抽象类。并且 getWindow()返回的仅仅是一个mWindow。

public Window getWindow() {

        return mWindow;

    }

那么mWindow是在什么地方初始化的。我们继续查看源代码,终于发现是在一个叫attach的方法中调用,但是终于我们没有发现调用attach的方法,事实上这种方法是在ActivityThread这个类中调用的。这个类非常重要,包含在main方法中初始化Looper,在这里先不说,以后在介绍。

private Activity performLaunchActivity(ActivityClientRecord r, Intent customIntent) {

…………

            Activity activity = null;

            activity = mInstrumentation.newActivity(cl, component.getClassName(), r.intent);

…………

Application app = r.packageInfo.makeApplication(false, mInstrumentation);

…………

activity.attach(appContext, this, getInstrumentation(), r.token,

                        r.ident, app, r.intent, r.activityInfo, title, r.parent,

                        r.embeddedID, r.lastNonConfigurationInstances, config);

…………

        return activity;

    }

接着我们在attach方法中看到这样一行代码

mWindow = PolicyManager.makeNewWindow(this);

继续查看,找到PolicyManager这个类,

private static final String POLICY_IMPL_CLASS_NAME =

        "com.android.internal.policy.impl.Policy";

…………

            Class policyClass = Class.forName(POLICY_IMPL_CLASS_NAME);

            sPolicy = (IPolicy)policyClass.newInstance();

  …………

    public static Window makeNewWindow(Context context) {

        return sPolicy.makeNewWindow(context);

    }

继续,在Policy这个类中我们找到

public Window makeNewWindow(Context context) {

        return new PhoneWindow(context);

    }

也就是说最初我们找到mWindow就是PhoneWindow,我们继续。不要闲着,

public class PhoneWindow extends Window implements MenuBuilder.Callback

我们通过源代码发现PhoneWindow事实上就是Window。先回到我们刚才说的

getWindow().setContentView(layoutResID);这种方法,事实上就是调用PhoneWindow的setContentView(layoutResID);方法。我们通过源代码继续查看

@Override

    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();

        }

    }

我们先简单分析一下下半部分。假设Callback不为空且没有被销毁的情况下会调用onContentChanged()方法。通过源代码我们发现事实上Activity是实现了Callback接口的

public class Activity extends ContextThemeWrapper

        implements LayoutInflater.Factory2,

        Window.Callback, KeyEvent.Callback,

        OnCreateContextMenuListener, ComponentCallbacks2,

        Window.OnWindowDismissedCallback

并且在onContentChanged()方法中是空实现,什么都没做。

public void onContentChanged() {

    }

我们再来分析上半部分,假设mContentParent 不为空的话,首先移除全部的子View,removeAllViews方法是ViewGroup类中,View中没有。我们能够先大致看一下。以后在做具体介绍

/**

     * Call this method to remove all child views from the

     * ViewGroup.

     * 

     * <p><strong>Note:</strong> do not invoke this method from

     * {@link #draw(android.graphics.Canvas)}, {@link #onDraw(android.graphics.Canvas)},

     * {@link #dispatchDraw(android.graphics.Canvas)} or any related method.</p>

     */

    public void removeAllViews() {

        removeAllViewsInLayout();

        requestLayout();

        invalidate(true);

    }

假设mContentParent 为空的话,调用  installDecor();进行初始化,接着我们看一下  installDecor();方法的详细实现。

private void installDecor() {

        if (mDecor == null) {

            mDecor = generateDecor();

          …………

        }

        if (mContentParent == null) {

            mContentParent = generateLayout(mDecor);



            mTitleView = (TextView)findViewById(com.android.internal.R.id.title);

         …………

                mActionBar = (ActionBarView) findViewById(com.android.internal.R.id.action_bar);

…………

        }

    }

首先推断mDecor是否为空,假设为空就调用 generateDecor();我们接着看它详细怎么实现

protected DecorView generateDecor() {

        return new DecorView(getContext(), -1);

    }

我们发现就一行代码,那么DecorView又是什么,我们继续查看,

private final class DecorView extends FrameLayout implements RootViewSurfaceTaker

最终找到了,原来继承的是FrameLayout ,看到这里可能有的人会恍然大悟,由于我们知道在Activity的setContentView中载入的根布局事实上就是FrameLayout 。这里的FrameLayout 有两部分组成,一部分是系统自定义的title。一部分是content,就是我们自定义的布局,还有,在之前假设我们不须要title的话,我们会在AcdroidMainfest中 android:theme="@android:style/Theme.NoTitleBar",或者在activity中设置requestWindowFeature(Window.FEATURE_NO_TITLE);可是要设置在setContentView之前,好了,上面的代码我们先分析到这,以后有时间还会接着继续写。欢迎拍砖。

Android setContentView方法解析(一)的更多相关文章

  1. 【Android 多媒体开发】 MediaPlayer 状态机 接口 方法 解析

    作者 : 韩曙亮 转载请著名出处 :  http://blog.csdn.net/shulianghan/article/details/38487967 一. MediaPlayer 状态机 介绍 ...

  2. Android Service完全解析,关于服务你所需知道的一切(下)

    转载请注册出处:http://blog.csdn.net/guolin_blog/article/details/9797169 在上一篇文章中,我们学习了Android Service相关的许多重要 ...

  3. Android Service完全解析,关于服务你所需知道的一切(上)

    转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/11952435 相信大多数朋友对Service这个名词都不会陌生,没错,一个老练的A ...

  4. Android IntentService完全解析 当Service遇到Handler

    一 概述 大家都清楚,在Android的开发中,凡是遇到耗时的操作尽可能的会交给Service去做,比如我们上传多张图,上传的过程用户可能将应用置于后台,然后干别的去了,我们的Activity就很可能 ...

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

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

  6. Android实现XML解析技术

    转载:Android实现XML解析技术 本文介绍在Android平台中实现对XML的三种解析方式. XML在各种开发中都广泛应用,Android也不例外.作为承载数据的一个重要角色,如何读写XML成为 ...

  7. 36、Android Bitmap 全面解析

    Android Bitmap 全面解析(一)加载大尺寸图片 http://www.eoeandroid.com/thread-331669-1-1.html Android Bitmap 全面解析(二 ...

  8. 【转】Android Service完全解析,关于服务你所需知道的一切(下) ---- 不错

    原文网址:http://blog.csdn.net/guolin_blog/article/details/9797169 转载请注册出处:http://blog.csdn.net/guolin_bl ...

  9. Android ActionBar完全解析,使用官方推荐的最佳导航栏(上)

    转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/18234477 本篇文章主要内容来自于Android Doc,我翻译之后又做了些加工 ...

随机推荐

  1. Javascript数组求和的方法总结 以及由斐波那契数列得到的启发

    一次面试中,面试官要求用三种不同的Javascript方法进行一个数字数组的求和,当时思来想去只想到了使用循环这一种笨方法,因此面试比较失败,在这里总结了六种Javascript进行数组求和的方法,以 ...

  2. curl模拟带验证码的登录

    首先说明,不是用php自动识别验证码,而是有验证码的情况下,让你通过curl 带着cookies去请求远程资源,从而通过合法的身份验证.主要用来抓取需要登录后才能访问的资源. 思路就是获取到验证码之后 ...

  3. Python 解LeetCode:654. Maximum Binary Tree

    用一个整型数组构建一个二叉树,根结点是数组中的最大值,左右子树分别是根结点的值在数组中左右两边的部分. 分析,这是二叉树中比较容易想到的问题了,直接使用递归就行了,代码如下: class Soluti ...

  4. 简单聊聊java中如何判定一个对象可回收

    背景 说到java的特性,其中一个最重要的特性便是java通过new在堆中分配给对象的内存,不需要程序员主动去释放,而是由java虚拟机自动的回收.这也是java和C++的主要区别之一:那么虚拟机是如 ...

  5. H5定位

    百度地图javaScript API 一.在html文件中引入 <script src="http://api.map.baidu.com/api?ak=Uk9tDddYkrQImXw ...

  6. python基础-------模块与包(四)

    configparser模块与 subprcess 利用configparser模块配置一个类似于 windows.ini格式的文件可以包含一个或多个节(section),每个节可以有多个参数(键=值 ...

  7. 《Linux命令行与shell脚本编程大全》第十四章 处理用户输入

    有时还会需要脚本能够与使用者交互.bash shell提供了一些不同的方法来从用户处获得数据, 包括命令行参数,命令行选项,以及直接从键盘读取输入的能力. 14.1 命令行参数 就是添加在命令后的数据 ...

  8. 用Python写一款属于自己的 简易zip压缩软件 附完成图(适合初学者)

    一.软件描述 用Python tkinter模块写一款属于自己的压缩软件.zip文件格式是通用的文档压缩标准,在ziplib模块中,使用ZipFile来操作zip文件,具有功能:zip压缩功能,zip ...

  9. 秒表计时器以及Stopwatch

    Stopwatch:秒表计时器,用来记录程序的运行时间,通常用来测试代码在时间上的执行效率.(需要引用:System.Diagnostics.) Stopwatch sw=new Stopwatch( ...

  10. "逃离北京"的这些年 2

    一  找工作第二阶段 我为了保险,在辞职信还特别写了:特此提前一个月提出辞职. 果然是搞金融的,C公司在我提交辞职信后,一周内就让我整理好工作资料,办好辞职手续. 没关系,都是要走的人.早点离开也是好 ...