在看setContentView源码之前,就知道,PhoneWindow, DecorView这些东西,不知道之间的联系,上周末加班看了下源码,慷慨颇多,“源码面前,无所遁形。”
Hierarchy View 的使用
在看是之前我们先看下Android studio的Hierarchy View, 这是AS自带的View调试工具,怎样使用呢?
打开步骤:Tools —— Android ——Android Device Monitor —— window —— open persepctive :
此时我们的MainActivity的XML文件是这样的:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.liumengqiang.viewproject.MainActivity">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/aaaaaaaaaaaa"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@drawable/drawable_select"
android:padding="10dp"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</RelativeLayout>
</LinearLayout>
那么此时我们的View树是这样的:
此时看这图可能是一脸懵逼,分析完源码,我详细应该能看懂的。
源码开始:
setContentView(R.layout.activity_main);
我们的Activity继承自AppCompatActivity,:
@Override
public void setContentView(int layoutResID) {
getDelegate().setContentView(layoutResID);
}
getDelegate()是什么呢?
@NonNull
public AppCompatDelegate getDelegate() {
if (mDelegate == null) {
mDelegate = AppCompatDelegate.create(this, this);
}
return mDelegate;
}
进入create方法:
public static AppCompatDelegate create(Activity activity, AppCompatCallback callback) {
return create(activity, activity.getWindow(), callback);
}
private static AppCompatDelegate create(Context context, Window window,
AppCompatCallback callback) {
if (BuildCompat.isAtLeastO()) {
return new AppCompatDelegateImplO(context, window, callback);
} else if (Build.VERSION.SDK_INT >= 24) {
return new AppCompatDelegateImplN(context, window, callback);
} else if (Build.VERSION.SDK_INT >= 23) {
return new AppCompatDelegateImplV23(context, window, callback);
} else if (Build.VERSION.SDK_INT >= 14) {
return new AppCompatDelegateImplV14(context, window, callback);
} else if (Build.VERSION.SDK_INT >= 11) {
return new AppCompatDelegateImplV11(context, window, callback);
} else {
return new AppCompatDelegateImplV9(context, window, callback);
}
}
可以看到,根据SDK版本的不同,返回不同的AppCompatDelegateImplO继承自AppCompatDelegateImplN,AppCompatDelegateImplN继承自AppCompatDelegateImplV23,以此类推,最后都继承自AppCompatDelegateImplV9。那么getDelegate().setContentView
调用的肯定是父类的方法,最后在AppCompatDelegateImplV9类中找到了setContentView重载方法,可自行查找。源码如下:
@Override
public void setContentView(int resId) {
ensureSubDecor();
ViewGroup contentParent = (ViewGroup) mSubDecor.findViewById(android.R.id.content);
contentParent.removeAllViews();
LayoutInflater.from(mContext).inflate(resId, contentParent);
mOriginalWindowCallback.onContentChanged();
}
从代码可以暂时看出,经过ensureSubDecor()方法之后,mSubDecor.findViewById,返回一个ViewGroup, 然后contentParent作为跟布局,将我们的自己定义的布局添加到此contentParent中,至于contentParent和mSubDecor是什么,下文讲到。接下来进入ensureSubDecor方法:
private void ensureSubDecor() {
//初始化进入,mSubDecorInstalled为false, 所以执行createSubDecor方法
if (!mSubDecorInstalled) {
mSubDecor = createSubDecor();
.....
mSubDecorInstalled = true;
....
}
初始化进入该方法时,mSubDecorInstalled是false,所以进入该方法,在if判断内,将