DecorView对象的创建详解

本文详细探讨了DecorView的创建过程,从Activity的setContentView方法开始,讲解了PhoneWindow如何通过installDecor和generateDecor创建DecorView,以及DecorView的结构初始化。通过inflate方法将布局文件添加到mContentParent,最终在ViewRootImpl中完成添加和显示到屏幕的步骤,涉及measure、layout和draw的执行。
摘要由CSDN通过智能技术生成

DecorVIew 的创建

我们通过一个示例来看看顶层View(DecorView)是怎么创建出来的。

示例:

public class MainActivity extends Activity {
   

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }
  }  

在上面我们一般都会调用setContentView方法来设置布局。该方法调用了Activity的setContentView方法,我们去看看:

public class Activity extends ContextThemeWrapper
        implements LayoutInflater.Factory2,
        Window.Callback, KeyEvent.Callback,
        OnCreateContextMenuListener, ComponentCallbacks2,
        Window.OnWindowDismissedCallback {
   

.......

 public void setContentView(int layoutResID) {
        getWindow().setContentView(layoutResID);//调用与之关联Window中的setContentView()方法
        initWindowDecorActionBar();
    }

......

 }

在父类Activity中其实就是调用与之关联的Window(真正的对象类型是PhoneWindow)setContentView()来创建DecorView对象。进入源码看看实现的过程:

进入Window类(实现对象类型是PhoneWindow)的setContentView():

 public class PhoneWindow extends Window implements MenuBuilder.Callback {
   

     ......

    public void setContentView(int layoutResID) {
        if (mContentParent == null) {
            installDecor();
        } else {
            mContentParent.removeAllViews();
        }
        mLayoutInflater.inflate(layoutResID, mContentParent);
        
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值