美文网首页
View的绘制流程二、setContentView

View的绘制流程二、setContentView

作者: b42f47238b43 | 来源:发表于2017-11-07 00:11 被阅读0次

View的绘制流程一中我们已经了解了View是怎样添加到父容器中的,而在Activity中布局是怎样被添加进去的呢?是不是和View的添加流程一样呢?带着疑问我们跟着源码来看一下布局是怎样被添加到Activity中的吧
注:现如今的 Android 开发过程中大多都会继承 AppCompatActivity 所以本文是以 AppCompatActivity 来讲解的

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}
@Override
public void setContentView(@LayoutRes int layoutResID) {
    getDelegate().setContentView(layoutResID);
}
@NonNull
public AppCompatDelegate getDelegate() {
    if (mDelegate == null) {
        mDelegate = AppCompatDelegate.create(this, this);
    }
    return mDelegate;
}
public static AppCompatDelegate create(Activity activity, AppCompatCallback callback) {
    return create(activity, activity.getWindow(), callback);
}
private static AppCompatDelegate create(Context context, Window window,
        AppCompatCallback callback) {
    final int sdk = Build.VERSION.SDK_INT;
    if (BuildCompat.isAtLeastN()) {
        return new AppCompatDelegateImplN(context, window, callback);
    } else if (sdk >= 23) {
        return new AppCompatDelegateImplV23(context, window, callback);
    } else if (sdk >= 14) {
        return new AppCompatDelegateImplV14(context, window, callback);
    } else if (sdk >= 11) {
        return new AppCompatDelegateImplV11(context, window, callback);
    } else {
        return new AppCompatDelegateImplV9(context, window, callback);
    }
}

上面一些简单的代码追踪可以看出谷歌工程师为了版本兼容花费了很多心思。针对不同的版本重写了各种AppCompatDelegate子类,其中AppCompatDelegateImplN继承AppCompatDelegateImplV23AppCompatDelegateImplV23继承AppCompatDelegateImplV14AppCompatDelegateImplV14继承AppCompatDelegateImplV11AppCompatDelegateImplV11继承AppCompatDelegateImplV9AppCompatDelegateImplV9重写了setContentView()方法所以getDelegate().setContentView(view);最终会调用AppCompatDelegateImplV9setContentView()方法

@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();方法之后获取一个contentParent的父容器,移除父容器中所有的子View然后将Activity中设置的资源文件添加到这个父容器之后通知一下布局发生改变。那么mSubDecor是什么呢andorid.R.id.content这个id又是什么呢为什么要将布局添加到contentParent中去呢?带着疑问我们继续来分析ensureSubDecor()中代码

private void ensureSubDecor() {
    if (!mSubDecorInstalled) {
        //创建SubDecor
        mSubDecor = createSubDecor();
        // If a title was set before we installed the decor, propagate it now
        CharSequence title = getTitle();
        if (!TextUtils.isEmpty(title)) {
            onTitleChanged(title);
        }
        applyFixedSizeWindow();
        onSubDecorInstalled(mSubDecor);
        mSubDecorInstalled = true;
        // Invalidate if the panel menu hasn't been created before this.
        // Panel menu invalidation is deferred avoiding application onCreateOptionsMenu
        // being called in the middle of onCreate or similar.
        // A pending invalidation will typically be resolved before the posted message
        // would run normally in order to satisfy instance state restoration.
        PanelFeatureState st = getPanelState(FEATURE_OPTIONS_PANEL, false);
        if (!isDestroyed() && (st == null || st.menu == null)) {
            invalidatePanelMenu(FEATURE_SUPPORT_ACTION_BAR);
        }
    }
}

这里我们主要分析mSubDecor是怎样创建的,跟踪代码

/**
 * 该方法主要是进行版本兼容使用自己的subDecor来替换phoneWindow中的DecorView
 */
private ViewGroup createSubDecor() {
    //初始化一些style属性根据不同的属性兼容不同的初始布局
    TypedArray a = mContext.obtainStyledAttributes(R.styleable.AppCompatTheme);
    if (!a.hasValue(R.styleable.AppCompatTheme_windowActionBar)) {
        a.recycle();
        throw new IllegalStateException(
                "You need to use a Theme.AppCompat theme (or descendant) with this activity.");
    }
    //设置状态位。决定初始布局是否需要title、actionBar等等
    //以下都是对window的状态进行初始化设置并设置到feature属性
    if (a.getBoolean(R.styleable.AppCompatTheme_windowNoTitle, false)) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
    } else if (a.getBoolean(R.styleable.AppCompatTheme_windowActionBar, false)) {
        // Don't allow an action bar if there is no title.
        requestWindowFeature(FEATURE_SUPPORT_ACTION_BAR);
    }
    if (a.getBoolean(R.styleable.AppCompatTheme_windowActionBarOverlay, false)) {
        requestWindowFeature(FEATURE_SUPPORT_ACTION_BAR_OVERLAY);
    }
    if (a.getBoolean(R.styleable.AppCompatTheme_windowActionModeOverlay, false)) {
        requestWindowFeature(FEATURE_ACTION_MODE_OVERLAY);
    }
    //判断window是否是浮窗形式的
    mIsFloating = a.getBoolean(R.styleable.AppCompatTheme_android_windowIsFloating, false);
    a.recycle();
    // Now let's make sure that the Window has installed its decor by retrieving it
    //新建phoneWindow下的decorView
    mWindow.getDecorView();
    final LayoutInflater inflater = LayoutInflater.from(mContext);
    ViewGroup subDecor = null;
    //根据之前的feature状态属性来初始化subDecor的布局
    ...省略部分代码...
    //上面省略部分根据不同条件设置subDecor的代码。这里有个abc+screen_simple我们会分析一下这个布局的代码
    //其他布局道理也是一样的
    subDecor = (ViewGroup) inflater.inflate(R.layout.abc_screen_simple, null);
    ...省略部分代码...
    //下面代码就很重要了谷歌工程师为了进行版本适配在下面代码进行了偷梁换柱
    //获取contentVIew容器
    final ContentFrameLayout contentView = (ContentFrameLayout) subDecor.findViewById(
            R.id.action_bar_activity_content);
    //这里的windowContentView是decorView创建的。
    //上文中setContent方法中出现的android.R.id.content这里又出现了,在上面方法中可以知道activity
    //的资源布局就是添加到id为android.R.id.content的容器中的。
    final ViewGroup windowContentView = (ViewGroup) mWindow.findViewById(android.R.id.content);
    if (windowContentView != null) {
        // There might be Views already added to the Window's content view so we need to
        // migrate them to our content view
        //这里将windowContentView中所有的子View全部转移到contentView中去
        while (windowContentView.getChildCount() > 0) {
            final View child = windowContentView.getChildAt(0);
            windowContentView.removeViewAt(0);
            contentView.addView(child);
        }
        // Change our content FrameLayout to use the android.R.id.content id.
        // Useful for fragments.
        //这里巧妙的将android.R.id.content从windowContentView转移到contentView中实现了subDecor
        //到decorView的替换
        windowContentView.setId(View.NO_ID);
        contentView.setId(android.R.id.content);
        // The decorContent may have a foreground drawable set (windowContentOverlay).
        // Remove this as we handle it ourselves
        if (windowContentView instanceof FrameLayout) {
            ((FrameLayout) windowContentView).setForeground(null);
        }
    }
    // Now set the Window's content view with the decor
    //将subDecor添加到windowContentView中
    mWindow.setContentView(subDecor);
    return subDecor;
}

上述代码是整个setContentView流程的核心,这里主要就是把phoneWindow中创建的DecorView的内容转移到自己的subDecor中去

<android.support.v7.widget.FitWindowsLinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/action_bar_root"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:fitsSystemWindows="true">

    <android.support.v7.widget.ViewStubCompat
        android:id="@+id/action_mode_bar_stub"
        android:inflatedId="@+id/action_mode_bar"
        android:layout="@layout/abc_action_mode_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <android.support.v7.widget.ContentFrameLayout
            android:id="@id/action_bar_activity_content"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:foregroundGravity="fill_horizontal|top"
            android:foreground="?android:attr/windowContentOverlay" />
</android.support.v7.widget.FitWindowsLinearLayout>

上面对应的是abc_screen_simple的代码很简单就不做分析了

@Override
public final View getDecorView() {
    if (mDecor == null || mForceDecorInstall) {
        installDecor();
    }
    return mDecor;
}
private void installDecor() {
    mForceDecorInstall = false;
    if (mDecor == null) {
        //创建decorView对象mDecor
        mDecor = generateDecor(-1);
        mDecor.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
        mDecor.setIsRootNamespace(true);
        if (!mInvalidatePanelMenuPosted && mInvalidatePanelMenuFeatures != 0) {
            mDecor.postOnAnimation(mInvalidatePanelMenuRunnable);
        }
    } else {
        mDecor.setWindow(this);
    }
    if (mContentParent == null) {
        //这里初始化mDecor布局,并且通过android.R.id.content ID找到mContentParent布局
        //具体初始化流程跟上面的createSubDecor方法类似就不做分析了
        mContentParent = generateLayout(mDecor);
        ...省略部分代码...
        //剩下代码主要做一些样式初始化和设置就不进行分析了
protected DecorView generateDecor(int featureId) {
    // System process doesn't have application context and in that case we need to directly use
    // the context we have. Otherwise we want the application context, so we don't cling to the
    // activity.
    Context context;
    if (mUseDecorContext) {
        Context applicationContext = getContext().getApplicationContext();
        if (applicationContext == null) {
            context = getContext();
        } else {
            context = new DecorContext(applicationContext, getContext().getResources());
            if (mTheme != -1) {
                context.setTheme(mTheme);
            }
        }
    } else {
        context = getContext();
    }
    return new DecorView(context, featureId, this, getAttributes());
}

总结:

到此我们的setContentView流程就已经分析结束了,从上面的代码中我们可以分析出AppCompatActivity的布局嵌套为


image.png

相关文章

网友评论

      本文标题:View的绘制流程二、setContentView

      本文链接:https://www.haomeiwen.com/subject/tyyxmxtx.html