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 ![无标题.png](http://upload-images.jianshu.io/upload_images/4658633-9a3a758cbac9f8aa.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) ]) + "=" + 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)的一个实例
所以最终的图形就是

 
 
 
 
 

 

转载于:https://www.cnblogs.com/lianzhen/p/11238301.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值