setContentView大家都不陌生,他是我们平常写页面必须要实现的方法,也不废话,直接上图上代码,本文讲分Activity和AppCompatActivity来讲解setContentView
1.首先直接看Activity的setContentView方法

通过window来调用setContentView的方法,那么window又是什么呢? 这个window其实是一个phoneWindow,在activity的attach创建了window=new PhoneWindow(),至于attach什么调用的后面给大家分析.居然是PhoneWindow,那么直接看PhoneWindow的setContentView方法
1.PhoneWindow.setContentView

首先第一步会调用installDecor方法,因为mDecor是null,所以会调用generateDecor方法创建一个mDecor,然后调用generateLayout方法,generateLayout方法会根据标记去设置各种各样的title,然后创建一个view赋值给generateLayout

generateLayout代码比较长,就拿一个简单说一下,首先是根据设置的标记创建相应的布局,例如R.layout.screen_simple布局然后onResourcesLoaded方法它是将R.layout.screen_simple布局渲染到我们的Decor上面去.,然后拿到id, ID_ANDROID_CONTENT==com.android.internal.R.id.content,再然后通过mLayoutInflater.inflate(layoutResID, mContentParent);渲染我们自己的写的布局


总结一下:activity里面创建了一个window,window里面创建了一个DecorView,DecorView里面有个xml布局,这个xml布局有个FrameLayout 他的id是content,通过获取id+content将我们自己activity_main布局添加到这个FrameLayout,最后将xml布局添加到DecorView上面去

2.AppCompatActivity的setContentView方法

getDelegate()调用的是AppCompatDelegateImpl,所以直接看AppCompatDelegateImpl的setContentView方法.首先第一步先分析ensureSubDecor,ensureSubDecor先会调用createSubDecor方法


createSubDecor首先首先会根据设置加载不同的主题样式,然后调用ensureWindow,需要注意的是这里调用ensureWindow的目的只是判空的,因为在onCreate的时候就已经调用了ensureWindow方法创建了mWindow,然后通过mWindow,拿到DecorView. mWindow就是PhoneWindow


然后再回到createSubDecor方法,根据不同状态设置相应的subDecor,例如加载abc_screen_simple布局



加载布局之后呢,通过获取id拿到这个ContentFrameLayout,然后又通过mWindow拿到ViewGorup,这里的mWindow拿到的就是上面的R.layout.screen_simple布局的id,然后进行循环移除screen_simple里面的子view,并且把移除的这个view添加的到abc_screen_simple里面去,然后将ViewGorup的id设置为空,然后设置contentView的id
可以这么理解原始的R.layout.screen_simple里面有一个id.content,然后我把它的id设置为空,然后将R.layout.abc_screen_simple的里面的ID设置为id.content,也就是相当于在R.layout.screen_simple又包裹了abc_screen_simple,接着调用mWindow.setContentView(subDecor);

通过上述的流程就拿到了mContentParent了,然后调用了PhoneWindow.setContentView(view)通过addView添加到mContentParent里面,接着回到就执行了LayoutInflater.from(mContext).inflate(resId, contentParent),进行布局的填充

总结一下:大致流程和activity里面的流程差不多,也是拿到window,window里面创建了一个DecorView,DecorView里面有个abc_screen_simple.xml布局,这个abc_screen_simple.xml布局有个ContentFrameLayout 他的初始id是action_bar_activity_content,然后在获取R.layout.screen_simple的id.content,先是遍历移除screen_simple的子view,然后将移除的view添加abc_screen_simple里面去,最后讲screen_simple的id设置为空,把screen_simple的id设置给abc_screen_simple,这里主要是兼容性,所以将id设置为screen_simple的id,最后是LayoutInflater.from(mContext).inflate(resId, contentParent),进行布局的填充

3.LayoutInflater.from(mContext).inflate(resId, contentParent)
会先进行xml的解析,然后调用inflate方法,在inflate里面调用createViewFromTag方法,这个方法注意目的就是创建rootView

LayoutInflater.createViewFromTag方法怎么创建rootView的呢?首先通过判断是否包含indexOf判断来创建不同的view,其目的就是区分是否是自定义view,然后将其包名补全.举个例子说明下androidx.constraintlayout.widget.ConstraintLayout进入了if了

经过周转最后调用了PhoneLayoutInflater的onCreateView方法,它for循环调用了createView方法

通过反射创建view对象

4.创建子View
viewRoot创建之后后,我们还需要创建子view,我们回到inflate方法里面的rInflate方法,在rlnflate里面根据不同的类型执行相应的操作,为了方便我们直接看createViewFromTag方法,这个方法是不是很熟悉,之前调用过了,创建createViewFromTag之后设置参数,然后通过addView添加到viewGroup里面去

setContentView分析到这里就大致分析完了,有问题提出问题改正
网友评论