Activity视图结构学习

通过本文你可能会明白以下几点

  • 1 .setContentView是否可以多次调用
  • 2 .为什么requestWindowFeature要在setContentView之前调用
  • 3 .关于Activity完整视图结构的概念

可能很多人都看过和下面类似的图:


这才是我们一个Activity的完整视图,我们调用setContentView时,只是将布局放到content中了,何以见得呢,可以参考源码。关于setContentView的具体流程可以参考我的这篇文章

setContentView最后调用了PhoneWindow的setContentView方法

frameworks\base\core\java\com\android\internal\policy\PhoneWindow.java
    @Override
    public void setContentView(int layoutResID) {
        if (mContentParent == null) {
            installDecor();
        } else if (!hasFeature(FEATURE_CONTENT_TRANSITIONS)) {
            mContentParent.removeAllViews();
        }

        if (hasFeature(FEATURE_CONTENT_TRANSITIONS)) {
            final Scene newScene = Scene.getSceneForLayout(mContentParent, layoutResID,
                    getContext());
            transitionTo(newScene);
        } else {
            mLayoutInflater.inflate(layoutResID, mContentParent);
        }
        mContentParent.requestApplyInsets();
        final Callback cb = getCallback();
        if (cb != null && !isDestroyed()) {
            cb.onContentChanged();
        }
        mContentParentExplicitlySet = true;
    }

这里调用了mLayoutInflater.inflate来加载布局,加载到的布局最终通过addView添加到了mContentParent中(详细过程见这里)。mContentParent是什么呢?它是一个ViewGroup类型的成员变量。在这个方法开头对他进行的为空判断,为空时会调用installDecor方法,不为空时先清空其子view(看到这里也就说明activity中setContentView可以多次调用来加载不同布局)。我们来看installDecor

    private void installDecor() {
        mForceDecorInstall = false;
        if (mDecor == null) {
            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) {
            mContentParent = generateLayout(mDecor);

      ....
}

这个方法比较长仅截取相关片段。这里并没有着急初始化mContentParent ,而是初始化了mDecor 。mDecor 就是一个DecorView对象。这里第一次出现了DecorView,DecorView继承于FrameLayout,是一个Activity的顶级视图,也就是最外层的布局,如本文开头图中所示。先不直接去看这个类,先看看初始化mDecor 的那个方法:

    protected DecorView generateDecor(int featureId) {
        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());
    }

可见仅仅只是简单new了一个DecorView而已。我们接着看installDecor,在初始化完mDecor 之后,就开始初始化mContentParent 了,调用了generateLayout方法:

    protected ViewGroup generateLayout(DecorView decor) {
         ....

        int layoutResource;
        int features = getLocalFeatures();
        if ((features & (1 << FEATURE_SWIPE_TO_DISMISS)) != 0) {
            layoutResource = R.layout.screen_swipe_dismiss;
            setCloseOnSwipeEnabled(true);
        } else if ((features & ((1 << FEATURE_LEFT_ICON) | (1 << FEATURE_RIGHT_ICON))) != 0) {
            if (mIsFloating) {
                TypedValue res = new TypedValue();
                getContext().getTheme().resolveAttribute(
                        R.attr.dialogTitleIconsDecorLayout, res, true);
                layoutResource = res.resourceId;
            } else {
                layoutResource = R.layout.screen_title_icons;
            }
            removeFeature(FEATURE_ACTION_BAR);
        } else if ((features & ((1 << FEATURE_PROGRESS) | (1 << FEATURE_INDETERMINATE_PROGRESS))) != 0
                && (features & (1 << FEATURE_ACTION_BAR)) == 0) {
            layoutResource = R.layout.screen_progress;
        } else if ((features & (1 << FEATURE_CUSTOM_TITLE)) != 0) {
            if (mIsFloating) {
                TypedValue res = new TypedValue();
                getContext().getTheme().resolveAttribute(
                        R.attr.dialogCustomTitleDecorLayout, res, true);
                layoutResource = res.resourceId;
            } else {
                layoutResource = R.layout.screen_custom_title;
            }
            removeFeature(FEATURE_ACTION_BAR);
        } else if ((features & (1 << FEATURE_NO_TITLE)) == 0) {
            if (mIsFloating) {
                TypedValue res = new TypedValue();
                getContext().getTheme().resolveAttribute(
                        R.attr.dialogTitleDecorLayout, res, true);
                layoutResource = res.resourceId;
            } else if ((features & (1 << FEATURE_ACTION_BAR)) != 0) {
                layoutResource = a.getResourceId(
                        R.styleable.Window_windowActionBarFullscreenDecorLayout,
                        R.layout.screen_action_bar);
            } else {
                layoutResource = R.layout.screen_title;
            }
        } else if ((features & (1 << FEATURE_ACTION_MODE_OVERLAY)) != 0) {
            layoutResource = R.layout.screen_simple_overlay_action_mode;
        } else {
            layoutResource = R.layout.screen_simple;
        }

        mDecor.startChanging();
        mDecor.onResourcesLoaded(mLayoutInflater, layoutResource);

        ViewGroup contentParent = (ViewGroup)findViewById(ID_ANDROID_CONTENT);
        ....

        return contentParent;
    }

这个方法也非常长,这里截取一些重要地方。截取的代码中,先是在选布局,依据的就是不同的features ,看一下getLocalFeatures方法,这里PhoneWindow没有实现这个方法,调用的时父类,也就是Window中的:

android\frameworks\base\core\java\android\view\Window.java
    protected final int getLocalFeatures()
    {
        return mLocalFeatures;
    }

这里仅是取了mLocalFeatures的值,有一个设置的地方就在requestFeature

    public boolean requestFeature(int featureId) {
        final int flag = 1<<featureId;
        mFeatures |= flag;
        mLocalFeatures |= mContainer != null ? (flag&~mContainer.mFeatures) : flag;
        return (mFeatures&flag) != 0;
    }

看到这里,应该就明白了,为什么我们在取消标题栏时,requestWindowFeature(Window.FEATURE_NO_TITLE);的调用必须放在setContentView之前才会生效,因为setContentView执行过程中,会根据features 选择DecorView的布局。

我们可以看一下加载的布局,最最常用的R.layout.screen_title:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:fitsSystemWindows="true">
    <ViewStub android:id="@+id/action_mode_bar_stub"
              android:inflatedId="@+id/action_mode_bar"
              android:layout="@layout/action_mode_bar"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:theme="?attr/actionBarTheme" />
    <FrameLayout
        android:layout_width="match_parent" 
        android:layout_height="?android:attr/windowTitleSize"
        style="?android:attr/windowTitleBackgroundStyle">
        <TextView android:id="@android:id/title" 
            style="?android:attr/windowTitleStyle"
            android:background="@null"
            android:fadingEdge="horizontal"
            android:gravity="center_vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </FrameLayout>
    <FrameLayout android:id="@android:id/content"
        android:layout_width="match_parent" 
        android:layout_height="0dip"
        android:layout_weight="1"
        android:foregroundGravity="fill_horizontal|top"
        android:foreground="?android:attr/windowContentOverlay" />
</LinearLayout>

全屏时布局R.layout.screen_simple

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    android:orientation="vertical">
    <ViewStub android:id="@+id/action_mode_bar_stub"
              android:inflatedId="@+id/action_mode_bar"
              android:layout="@layout/action_mode_bar"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:theme="?attr/actionBarTheme" />
    <FrameLayout
         android:id="@android:id/content"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:foregroundInsidePadding="false"
         android:foregroundGravity="fill_horizontal|top"
         android:foreground="?android:attr/windowContentOverlay" />
</LinearLayout>

可见全屏时,就是去掉了@android:id/title,正常时是一个LinearLayout。自上到下是@android:id/title和@android:id/content。和我们文章开头的图中一样。

确定完布局后就调用了DecorView的onResourcesLoaded方法把布局添加到DecorView中,不要忘了DecorView是一个FrameLayout,开始里面是没有任何东西的。

添加完布局后,紧接着就调用了findViewById方法,那个常量ID_ANDROID_CONTENT在父类Window中定义着。之后在generateLayout方法最后返回了contentParent 。最后在setContentView中,将布局加载到这个FrameLayout中。至于title,在installDecor中根据实际情况实例化了mTitleView,他的title由setTitle设置,我们在activity中调用的setTitle会最后间接调用PhoneWindow中的setTitle。

setContentView执行完毕后,我们一个activity的完整视图就完成了。总结一下,最外层为一个DecorView,他是一个FrameLayout,内部根据具体情况加载不同布局,但必有一个id为@android:id/content的FrameLayout,这里用来加载我们自己要显示的布局。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容