大家都知道在Activity的onCreate()中调用Activity.setContent()方法能够载入布局文件以设置该Activity的显示界面。本文将从setContentView()的源代码谈起。分析布局文件载入所涉及到的调用链。

本文所用的源代码为android-19.

Step 1  、Activity.setContentView(intresId)

public void setContentView(int layoutResID) {
getWindow().setContentView(layoutResID);
initActionBar();
} public Window getWindow() {
return mWindow;
}

该方法调用了该Activity成员的mWindow,mWindow为Window对象。Windown对象是一个抽象类,提供了标准UI的显示策略和行为策略。在SDK中仅仅有PhoneWindow类实现了Window类。而Window中的setContentView()为空函数,所以最后调用的是PhoneWindow对象的方法。

Step 2  、PhoneWindow.setContentView()

@Override
public void setContentView(int layoutResID) {
if (mContentParent == null) {
installDecor();
} else {
mContentParent.removeAllViews();
}
// 将布局文件加入到mContentParent中
mLayoutInflater.inflate(layoutResID, mContentParent);
final Callback cb = getCallback();
if (cb != null && !isDestroyed()) {
cb.onContentChanged();
}
}

该方法首先依据mContentParent是否为空对mContentParent进行对应的设置。mContentParent为ViewGroup类型,若其已经初始化了,则移除全部的子View。否则调用installDecor()初始化。

接着将资源文件转成View树,并加入到mContentParent视图中。

Step 3、 PhoneWindow.installDecor() 

这段代码比較长,以下的伪代码仅仅介绍逻辑,读者可自行查看源代码。

private void installDecor() {
if (mDecor == null) {
/*创建一个DecorView对象并设置对应的属性。 DecorView是全部View的根View*/
mDecor = generateDecor();
mDecor.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
mDecor.setIsRootNamespace(true);
if (!mInvalidatePanelMenuPosted && mInvalidatePanelMenuFeatures != 0) {
mDecor.postOnAnimation(mInvalidatePanelMenuRunnable);
}
}
if (mContentParent == null) {
/*创建mContentParent对象*/
mContentParent = generateLayout(mDecor); // Set up decor part of UI to ignore fitsSystemWindows if appropriate.
mDecor.makeOptionalFitsSystemWindows(); mTitleView = (TextView)findViewById(com.android.internal.R.id.title);
if (mTitleView != null) { mTitleView.setLayoutDirection(mDecor.getLayoutDirection());
// 依据features值,设置Title的相关属性
if ((getLocalFeatures() & (1 << FEATURE_NO_TITLE)) != 0) {
View titleContainer = findViewById(com.android.internal.R.id.title_container);
if (titleContainer != null) {
titleContainer.setVisibility(View.GONE);
} else {
mTitleView.setVisibility(View.GONE);
}
if (mContentParent instanceof FrameLayout) {
((FrameLayout)mContentParent).setForeground(null);
}
} else {
mTitleView.setText(mTitle);
}
} else {
//若没有Title。则设置ActionBar的相关属性。如回调函数、风格属性
mActionBar = (ActionBarView) findViewById(com.android.internal.R.id.action_bar);
if (mActionBar != null) {
//....... // 推迟调用invalidate,放置onCreateOptionsMenu在 onCreate的时候被调用
mDecor.post(new Runnable() {
public void run() {
// Invalidate if the panel menu hasn't been created before this.
PanelFeatureState st = getPanelState(FEATURE_OPTIONS_PANEL, false);
if (!isDestroyed() && (st == null || st.menu == null)) {
invalidatePanelMenu(FEATURE_ACTION_BAR);
}
}
});
}
}
}
}

创建mContentParent对象的代码例如以下:

protected ViewGroup generateLayout(DecorView decor) {
// Apply data from current theme.
//获取当前Window主题的属性数据,相关字段的文件位置为:sdk\platforms\android-19\data\res\values\attrs.xml
//这些属性值能够在AndroidManifest.xml中设置Activityandroid:theme="",也能够在Activity的onCreate中通过
//requestWindowFeature()来设置.注意,该方法一定要在setContentView之前被调用
TypedArray a = getWindowStyle();
//获取android:theme=""中设置的theme
//依据主题属性值来设置PhoneWindow的特征与布局。包含Title、ActionBar、ActionBar的模式、Window的尺寸等属性。
//........ // Inflate the window decor.
// 依据上面设置的Window feature来确定布局文件
// Android SDK内置布局目录位置为:sdk\platforms\android-19\data\res\layout
典型的窗体布局文件有:
R.layout.dialog_titile_icons R.layout.screen_title_icons
R.layout.screen_progress R.layout.dialog_custom_title
R.layout.dialog_title
R.layout.screen_title // 最经常使用的Activity窗体修饰布局文件
R.layout.screen_simple //全屏的Activity窗体布局文件 int layoutResource;
int features = getLocalFeatures(); //会调用requesetWindowFeature()
// ......
mDecor.startChanging();
// 将布局文件转换成View数。然后加入到DecorView中
View in = mLayoutInflater.inflate(layoutResource, null);
decor.addView(in, new ViewGroup.LayoutParams(MATCH_PARENT, MATCH_PARENT));
//取出作为Content的ViewGroup。 android:id="@android:id/content",是一个FrameLayout
ViewGroup contentParent = (ViewGroup)findViewById(ID_ANDROID_CONTENT);
if (contentParent == null) {
throw new RuntimeException("Window couldn't find content container view");
} if ((features & (1 << FEATURE_INDETERMINATE_PROGRESS)) != 0) {
ProgressBar progress = getCircularProgressBar(false);
if (progress != null) {
progress.setIndeterminate(true);
}
} // 将顶层Window的背景、标题、Frame等属性设置成曾经的
if (getContainer() == null) {
//......
} mDecor.finishChanging(); return contentParent;
}

总结:setContentView将布局文件载入到程序窗体的过程可概况为:

1、创建一个DecorView对象。该对象将作为整个应用窗体的根视图

2、依据android:theme=""或requestWindowFeature的设定值设置窗体的属性,依据这些属性选择载入系统内置的布局文件

3、从载入后的布局文件里取出id为content的FrameLayout来作为Content的Parent

4、将setContentView中传入的布局文件载入到3中取出的FrameLayout中





最后。当AMS(ActivityManagerService)准备resume一个Activity时,会回调该Activity的handleResumeActivity()方法,

该方法会调用Activity的makeVisible方法 ,显示我们刚才创建的mDecor视图族。

//系统resume一个Activity时,调用此方法
final void handleResumeActivity(IBinder token, boolean clearHide, boolean isForward) {
ActivityRecord r = performResumeActivity(token, clearHide);
//...
if (r.activity.mVisibleFromClient) {
r.activity.makeVisible();
}
}

makeVisible位于ActivityThread类中,代码例如以下:

void makeVisible() {
if (!mWindowAdded) {
ViewManager wm = getWindowManager(); // 获取WindowManager对象
wm.addView(mDecor, getWindow().getAttributes());
mWindowAdded = true;
}
mDecor.setVisibility(View.VISIBLE); //使其处于显示状况
}

參考文章:http://blog.csdn.net/qinjuning/article/details/7226787

Android布局文件的载入过程分析:Activity.setContentView()源代码分析的更多相关文章

  1. Xamarin Android布局文件没有智能提示

    Xamarin Android布局文件没有智能提示 在Visual Studio 2015中,Android项目的Main.axml文件没有智能提示,不便于布局文件的编写.解决办法:(1)从Xamar ...

  2. Android布局文件夹引起的问题

    Android 运行到setContentView(R.layout.splash); 总是出现如下的错误: java.lang.RuntimeException: Unable to start a ...

  3. NullPointerException空指针异常——没有事先加载布局文件到acitivy——缺少:setContentView(R.layout.activity_setup_over);

    空指针异常: 04-27 01:13:57.270: E/AndroidRuntime(4942): FATAL EXCEPTION: main04-27 01:13:57.270: E/Androi ...

  4. android 布局文件中控件ID、name标签属性的命名包含“@”、“.”、“+”等等符号的含义

    1. 在项目的根目录有个配置文件“AndroidManifest.xml”,是用来设置Activity的属性的如 <?xml version="1.0" encoding=& ...

  5. android 布局文件中xmlns:android="http://schemas.android.com/apk/res/android"

    http://blog.163.com/benben_long/blog/static/199458243201411394624170/ xmlns:android="http://sch ...

  6. Android之View绘制流程开胃菜---setContentView(...)详细分析

    版权声明:本文出自汪磊的博客,转载请务必注明出处. 1 为什么要分析setContentView方法 作为安卓开发者相信大部分都有意或者无意看过如下图示:PhoneWindow,DecorView这些 ...

  7. Android布局文件layout.xml的一些属性值

        第一类:属性值 true或者 false android:layout_centerHrizontal 水平居中 android:layout_centerVertical 垂直居中 andr ...

  8. Android布局文件-错误

    View requires API level 14 (current min is 8): <?xml version="1.0" encoding="utf-8 ...

  9. android 布局文件 ScrollView 中的 listView item 显示不全解决方案

    import android.content.Context;import android.util.AttributeSet;import android.widget.ListView; /** ...

随机推荐

  1. 函数式编程-只用"表达式",不用"语句"()

    把函数当作普通的运算符使用. 2. 只用"表达式",不用"语句"() "表达式"(expression)是一个单纯的运算过程,总是有返回值: ...

  2. MVC 单元测试xUnit初探

    对于.NET项目 Web Api的业务逻辑后台开发[特别是做Web Api接口]而言,编写单元测试用例,会极大的减轻代码帮助与运行的方式.然而使用测试框架,相对于自带的,我更加推荐是用xUnit.ne ...

  3. Node_进阶_8

    Node进阶第八天 一.复习 Node.js特点:单线程.异步I/O(非阻塞I/O).事件驱动(事件环). 适合的程序:就是没有太多的计算,I/O比较多的业务. 举例:留言本.考试系统.说说.图片裁切 ...

  4. hdu 1978 记忆化搜索

    注意: dp[i][j] 表示(i,j)这个点有多少种方式       mark[i][j]表示这个点是否走过  假设有直接返回dp[i][j]    dp的求法为全部梦走到点的dp的和 注意mark ...

  5. Project Euler:Problem 93 Arithmetic expressions

    By using each of the digits from the set, {1, 2, 3, 4}, exactly once, and making use of the four ari ...

  6. java 基本类型、包装类、字符串之间的转换

    1.基本类型和包装类 基本类型和包装类可通过自动装箱和拆箱实现. int i = 24; Integer a = new Integer(i); //手动装箱 Integer b = i; //自动装 ...

  7. BEGINNING SHAREPOINT&#174; 2013 DEVELOPMENT 第10章节--SP2013中OAuth概览 创建和管理应用程序身份

    BEGINNING SHAREPOINT® 2013 DEVELOPMENT 第10章节--SP2013中OAuth概览  创建和管理应用程序身份         在之前的部分.你看到应用程序怎样像用 ...

  8. MyEclipse完好提示配置

    MyEclipse完好提示配置 一般的,MyEclipse中的提示以"."后进行提示,不是非常完好.如今.改动提示配置,让提示更完好. 详细操作例如以下: 1.打开MyEclips ...

  9. 反弹木马——本质上就是一个开80端口的CS程序,伪造自己在浏览网页

    反弹端口型木马分析了防火墙的特性后发现:防火墙对于连入的链接往往会进行非常严格的过滤,但是对于连出的链接却疏于防范.于是,与一般的木马相反,反弹端口型木马的服务端(被控制端)使用主动端口,客户端(控制 ...

  10. hpuoj--校赛--考试来了(水题)

    问题 C: 感恩节KK专场--考试来了 时间限制: 1 Sec  内存限制: 128 MB 提交: 475  解决: 112 [提交][状态][讨论版] 题目描述 很多课程马上就结课了,随之而来的就是 ...