从setContentView揭开DecorView

1.简介

看源码时我们会经常遇到DecorView,那么这个DecorView到底是什么呢,我们来研究一下。

本文源码基于android 27

2.代码分析

通常,我们在Activity的onCreate()中都有这么一句代码:

	setContentView(R.layout.main_activity);

那么这代码到底是干了啥呢,我们点进去看下。

2.1 Activity的setContentView

    public void setContentView(@LayoutRes int layoutResID) {
   
        getWindow().setContentView(layoutResID);//调用window的setContentView
        initWindowDecorActionBar();//初始化ActionBar
    }
    
    private Window mWindow;
    
    public Window getWindow() {
      
     return mWindow;
    }

这个mWindow会在Activityattach方法中被赋值:

2.2 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,
            Window window, ActivityConfigCallback activityConfigCallback) {
   
        //...

        mWindow = new PhoneWindow(this, window, activityConfigCallback);
        
        //...
    }    

可以看到mWindow的具体实现为PhoneWindow,那么我们来看下PhoneWindow中的setContentView

2.3 PhoneWindow的setContentView


    // This is the view in which the window contents are placed. It is either
    // mDecor itself, or a child of mDecor where the contents go.
    //翻译:这是放置窗口内容的view。它是mDecor本身或者是mDecor的一个子元素。 
    //这句话后面解释
    ViewGroup mContentParent;
    
    @Override
    public void setContentView(int layoutResID) {
   

        if (mContentParent == null) {
   //如果mContentParent为空
            installDecor();//安装DecorView
        } else if (!hasFeature(FEATURE_CONTENT_T
  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值