Activity绘制流程(1)

setContentView(resId);

调用activity
里面的Activity.java

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

接着执行到phoneWindow的setContentView里面

@Override
    public void setContentView(int layoutResID) {
        ......

            //调用这个方法给mContentParent赋值
            installDecor();
        ......
        //调用这个方法加载我们传进来的布局
         mLayoutInflater.inflate(layoutResID, mContentParent);
        ......

    }

在该方法里,首先会调用installDecor();

private void installDecor() {

       ......

        //先生成一个decorView
       generateDecor(-1);

        .......

         //调用generateLayout给decorView添加布局
        mContentParent = generateLayout(mDecor);

        ......

    }
protected ViewGroup generateLayout(DecorView decor) {

        mDecor.startChanging();
        mDecor.onResourcesLoaded(mLayoutInflater, layoutResource);

        ViewGroup contentParent = (ViewGroup)findViewById(ID_ANDROID_CONTENT);
        .......

        mDecor.finishChanging();

        return contentParent;
}
void onResourcesLoaded(LayoutInflater inflater, int layoutResource) {
        ......

        final View root = inflater.inflate(layoutResource, null);
       ......


            addView(root, 0, new ViewGroup.LayoutParams(MATCH_PARENT, MATCH_PARENT));
        ......
        mContentRoot = (ViewGroup) root;
       ......
    }

可见activity到view的流程是:decorView -> mContentRoot -> mContentParent -> resId(我们自己的跟布局)

这里写图片描述

猜你喜欢

转载自blog.csdn.net/kakaxiqianxin/article/details/79992727