Activity、View、Window之间关系的分析

1460594-625f48fc9fdbd74b.jpg
看大家都放图,我也来一张

通常我们所看到的Activity和View最直观的关系是在onCreate()方法中设置setContentView(LayoutId),为activity设置布局文件,这样view就在界面上显示出来了。这个方法做的操作如下:

 /**
 * Set the activity content from a layout resource.  The resource will be
 * inflated, adding all top-level views to the activity.
 *
 * @param layoutResID Resource ID to be inflated.
 *
 * @see #setContentView(android.view.View)
 * @see #setContentView(android.view.View, android.view.ViewGroup.LayoutParams)
 */
public void setContentView(@LayoutRes int layoutResID) {
    //其实是调用了window(PhoneWindow)的setContentView方法
    getWindow().setContentView(layoutResID);
    initWindowDecorActionBar();
 }

 public Window getWindow() {
    return mWindow;
 }

activity的setContentView最终调用的是mWindow的setContentView方法。mWindow的初始化是在activity的attach()方法中做的。

 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) {
          。。。
    //这里进行了初始化,mWindow的对象其实是Window的子类PhoneWindow
    mWindow = new PhoneWindow(this);
    //此处省略n行代码
    。。。。
}

到这里说明了一点,activity的setContentView()是调用了PhoneWindow的setContentView().
接着上PhoneWindow的代码:

 public void setContentView(int layoutResID) {
    // Note: FEATURE_CONTENT_TRANSITIONS may be set in the process of installing the window
    // decor, when theme attributes and the like are crystalized. Do not check the feature
    // before this happens.
    if (mContentParent == null) {
      //1、初始化DecorView,生成mContentParent
        installDecor();
    } else if (!hasFeature(FEATURE_CONTENT_TRANSITIONS)) {
        mContentParent.removeAllViews();
    }
    //  这个先不考虑
    if (hasFeature(FEATURE_CONTENT_TRANSITIONS)) {
        final Scene newScene = Scene.getSceneForLayout(mContentParent, layoutResID,
                getContext());
        transitionTo(newScene);
    } else {
    //2、加载activity自己的布局
        mLayoutInflater.inflate(layoutResID, mContentParent);
    }
//省略n行代码
    。。。。。
}

phoneWindow中的setContentView()方法一共可以分为两步。第一步:初始化话DectorView 第二步:加载我们自己设定的布局。到这里加载activity自己的view就结束了(ps:至于涉及到FrameWork层的东西这里就不作描述了)。主要来看一下installDecor()的操作。

private void installDecor() {
    if (mDecor == null) {
      //  生成DecorView,一个继承了FrameLayout的view
      //private final class DecorView extends FrameLayout implements   RootViewSurfaceTaker {
        mDecor = generateDecor();
        //省略若干代码
    }
    if (mContentParent == null) {
        //为mContentParent 赋值
        mContentParent = generateLayout(mDecor);
    }
    //省略若干代码
}
//  生成DecorView
protected DecorView generateDecor() {
    return new DecorView(getContext(), -1);
}

这个方法里面首先生成了一个DecorView(继承了FrameLayout),然后调用了generateLayout(mDecor)为mContentParent 赋值。mContentParent 是做什么用的呢?还记得当年的夏雨荷吗?哦 不 是记得phoneWindow中的setContentView()中执行的mLayoutInflater.inflate(layoutResID, mContentParent)代码
吗。没错,这个mContentParent就是作为了activity自己的布局(也就是我们自己写的布局)的一个父布局而存在。

接下来就看一下这个mContentParent到底是谁吧。

 protected ViewGroup generateLayout(DecorView decor) {
    //依照惯例 依然是省略n行代码
    。。。。
    // Inflate the window decor.

    int layoutResource;
    int features = getLocalFeatures();
    //  根据不同的features来加载不同的布局
    // System.out.println("Features: 0x" + Integer.toHexString(features));
    if ((features & (1 << FEATURE_SWIPE_TO_DISMISS)) != 0) {
        layoutResource = R.layout.screen_swipe_dismiss;
    } else if ((features & ((1 << FEATURE_LEFT_ICON) | (1 << FEATURE_RIGHT_ICON))) != 0) {
        if (mIsFloating) {
            TypedValue res = new TypedValue();
            getContext().getTheme().resolveAttribute(
                    R.attr.dialogTitleIconsDecorLayout, res, true);
            layoutResource = res.resourceId;
        } else {
            layoutResource = R.layout.screen_title_icons;
        }
       。。。。。。
    } else {
        // Embedded, so no decoration is needed.
        layoutResource = R.layout.screen_simple;
        // System.out.println("Simple!");
    }

    mDecor.startChanging();
    //这里 根据layoutId(上面根据不同的features 赋值的layoutResource )加载布局
    View in = mLayoutInflater.inflate(layoutResource, null);
    //把加载的布局放入到decorview中
    decor.addView(in, new ViewGroup.LayoutParams(MATCH_PARENT, MATCH_PARENT));
    mContentRoot = (ViewGroup) in;

  //public static final int ID_ANDROID_CONTENT = com.android.internal.R.id.content;
  //查找id为ID_ANDROID_CONTENT的控件
    ViewGroup contentParent = (ViewGroup)findViewById(ID_ANDROID_CONTENT);
    if (contentParent == null) {
        throw new RuntimeException("Window couldn't find content container view");
    }
。。。。
//  返回id为ID_ANDROID_CONTENT的控件
 return contentParent;
}

上面的代码主要就是完成了加载系统的布局、把布局放入到dectorview中、查找出com.android.internal.R.id.content的控件。这个控件就是我们自己写的布局的父view。
到这里window、activity、view之间的关系就清楚了。
1、activity的attach方法中执行了window的初始化,window的实例为PhoneWindow。
2、activity的setContentView(ID)方法最终是调用的PhoneWindow的setContentView()方法。
3、PhoneWindow在执行setContentView()的过程中生成了一个frameLayout的子类DecorView.并且根据feature的类型加载了一个对应的系统布局,放入了decorview中。系统布局中有一个id为com.android.internal.R.id.content的framelayout,这个frameLayout作为一个父布局加载我们应用中自己定义的xml文件。

也就是说,我们所有看到的页面其实都是在window里面的,activity其实并不直接掌控view而是借助于window展现的view。下面是时候放出图了(这张图就清楚的展现了activity、view、window之间的关系)


1460594-c45a8a390072413d.jpg
1677180-10136019b1f4c254.jpg
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值