Activity中setContentView浅析

Activity中setContentView浅析protectedvoidonCreate(finalBundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}setContentView()方法会将我们的视图设置到哪儿去了?publicvoidsetCo…

大家好,又见面了,我是你们的朋友全栈君。

protected void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

setContentView()方法会将我们的视图设置到哪儿去了?

public void setContentView(@LayoutRes int layoutResID) {
    getWindow().setContentView(layoutResID);
    initWindowDecorActionBar();
}

Activity会调用当前Activity的Window的setContentView()方法,而Window类是一个抽象类,唯一实现类PhoneWindow。

PhoneWindwo对象在Activity中的attach()方法中创建。

 final void attach(Context context, ActivityThread aThread,
        Instrumentation instr, IBinder token, int ident,
        Application application, Intent intent, ActivityInfo info,
        CharSequence title, Activity parent, String id,
        NonConfigurationInstances lastNonConfigurationInstances,
        Configuration config, String referrer, IVoiceInteractor voiceInteractor,
        Window window) {
    attachBaseContext(context);

    mFragments.attachHost(null /*parent*/);

    mWindow = new PhoneWindow(this, window);
    mWindow.setWindowControllerCallback(this);
    mWindow.setCallback(this);
    mWindow.setOnWindowDismissedCallback(this);
    mWindow.getLayoutInflater().setPrivateFactory(this);
    //......
}

那我们接着看PhoneWindow的setContentView()方法。

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();
    }
}

当我们第一此setContentView()时,mContentParent为空,会调用installDecor()方法做些初始化工作。然后再将我们setContentView里的View加载到mContentParent上面去。

mLayoutInflater.inflate(layoutResID, mContentParent)

来看看installDecor()做了什么伟大的事情:

private DecorView mDecor;

private ViewGroup mContentParent;

private ViewGroup mContentRoot;

private void installDecor() {
    if (mDecor == null) {
        mDecor = generateDecor();
        mDecor.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
        mDecor.setIsRootNamespace(true);
        if (!mInvalidatePanelMenuPosted && mInvalidatePanelMenuFeatures != 0) {
            mDecor.postOnAnimation(mInvalidatePanelMenuRunnable);
        }
    }
    if (mContentParent == null) {
        mContentParent = generateLayout(mDecor);

        // Set up decor part of UI to ignore fitsSystemWindows if appropriate.
        mDecor.makeOptionalFitsSystemWindows();
        //....
    }
}

mDecor是DecorView类对象,而DecorView继承自FrameLayout,generateDecor()方法就是初始化创建一个空的FrameLayout。来看generateLayout()方法是怎样初始化我们需要的mContentParent的:

protected ViewGroup generateLayout(DecorView decor) {   
    TypedArray a = getWindowStyle();
    if (a.getBoolean(R.styleable.Window_windowNoTitle, false)) {
        requestFeature(FEATURE_NO_TITLE);
    } else if (a.getBoolean(R.styleable.Window_windowActionBar, false)) {
        requestFeature(FEATURE_ACTION_BAR);
    }

    if (a.getBoolean(R.styleable.Window_windowActionBarOverlay, false)) {
        requestFeature(FEATURE_ACTION_BAR_OVERLAY);
    }
    //....
    if ((features & (1 << FEATURE_SWIPE_TO_DISMISS)) != 0) {
        layoutResource = R.layout.screen_swipe_dismiss;
    } 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) {}
    //.....
    int layoutResource;
    //.....
    View in = mLayoutInflater.inflate(layoutResource, null);
    decor.addView(in, new ViewGroup.LayoutParams(MATCH_PARENT, MATCH_PARENT));

    mContentRoot = (ViewGroup) in;

    ViewGroup contentParent = (ViewGroup)findViewById(ID_ANDROID_CONTENT);
    //.....
    return contentParent;
 }

generateLayout()方法会根据我们Acivity主题样式,选择加载不同的系统布局资源,并将该视图添加到DecorView中去。

View in = mLayoutInflater.inflate(layoutResource, null);
decor.addView(in, new ViewGroup.LayoutParams(MATCH_PARENT, MATCH_PARENT));
mContentRoot = (ViewGroup) in;

无论选择哪一个布局资源里面都一个id名为content的FrameLayout。再从该布局中找到我们需要的contentParent。

(ViewGroup)findViewById(ID_ANDROID_CONTENT);

最后返回contentParent,将setContentView()中的视图加载到mContentParent上去。
这里写图片描述

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/153245.html原文链接:https://javaforall.net

(0)
上一篇 2022年6月26日 下午12:00
下一篇 2022年6月26日 下午12:00


相关推荐

  • 现在入场“养虾”,赚钱还是赔钱?

    现在入场“养虾”,赚钱还是赔钱?

    2026年3月15日
    3
  • PhpSpreadsheet_php标准输入流stdin

    PhpSpreadsheet_php标准输入流stdin实例化对象composer导包”phpoffice/phpspreadsheet”:”^1.11″,$spreadsheet=newSpreadsheet();$sheet=$spreadsheet->getActiveSheet();合并单元格$sheet->mergeCells(‘A1:J1’);设置单元格的内容$spreadsheet->getActiveSheet()->setCellValue(‘A2′,’序号’)-&

    2025年12月13日
    5
  • Cursor MCP 完整教程:让 AI 连接外部工具的配置方法

    Cursor MCP 完整教程:让 AI 连接外部工具的配置方法

    2026年3月16日
    1
  • hive的存储类型_4.2数据类型

    hive的存储类型_4.2数据类型了解Hive数据类型,是Hive编程的基础。使用hive建表,首先要明白hive常用的数据类型有哪些,可以存储哪些类型的数据。其实Hive支持关系型数据库中的大多数基本数据类型,且同时支持关系型数据库中少见的3种集合数类型(STRUCT,MAP,ARRAY)。然而学习技术最好的方式之一就是去查看官方文档。Hive关于数据类型官网地址:Hive官网关于数据类型的介绍…

    2026年2月10日
    3
  • SQL语句面试题目_sql基础知识面试题

    SQL语句面试题目_sql基础知识面试题我自己编辑总结的sql面试题目大全,也是每条都验证过的第一类:sql面试题(学生表_课程表_成绩表_教师表)表结构,节选自:http://www.cnblogs.com/qixuejia/p/3637735.html题目一,节选,自:https://wenku.baidu.com/view/cda288f1b90d6c85ed3ac671.html题目二,节选,自:http://ww…

    2022年8月29日
    8
  • MIPI协议(下)

    MIPI协议(下)GOON…完成mipi信号通道分配后,需要生成与物理层对接的时序、同步信号: MIPI规定,传输过程中,包内是200mV、包间以及包启动和包结束时是1.2V,两种不同的电压摆幅,需要两组不同的LVDS驱动电路在轮流切换工作;为了传输过程中各数据包之间的安全可靠过渡,从启动到数据开始传输,MIPI定义了比较长的可靠过渡时间,加起来最少也有600多ns;而且规定各个时间参数是可调的,所以…

    2022年4月30日
    73

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号