各位看官们,大家好,上一回中咱们说的是Android中View原理概述的例子,这一回咱们说的例子是View中的助手。闲话休提,言归正转。让我们一起Talk Android吧!
看官们,我们在前面章回中说过,View上承载了很多东西,于是需要一些助手帮忙,本章回中介绍的就是它的助手PhoneWindow。另外,在这里说明一下,PhoneWidonw是Activity的一部分,在这里我们把Activity看作是一种广义上的View,因此把PhoneWindow说成是View的助手。好了,我们开始正式介绍PhoneWindow。
它是一种Window,在源代码中Window是一个抽象类,而PhoneWindow实现了该类,因此可以把它当作Window对象来处理。在代码中它用来和WindowManager进行沟通,而且WindowManager就是通过PhoneWindow来管理Activity的。明白了它的含义后,我们看看它是如何和Activity关联起来的。
要说PhoneWindow和Activity如何关联,那么还得从Activity的创建说起,也就是Activity的onCreate()函数,它调用了Activity的setContentView()函数,PhoneWindow就是在这个函数中创建的,我们可以看看具体的源代码:(代码文件:frameworks/base/core/java/android/app/Activity.java)
public void setContentView(@LayoutRes int layoutResID) {
getWindow().setContentView(layoutResID);
initWindowDecorActionBar();
}
这里的getWindow返回的就是PhoneWinodw类型的对象,源代码如下:
public Window getWindow() {
return mWindow;
}
而mWidow就是PhoneWinodw类型的变量,它是在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, ActivityConfigCallback activityConfigCallback) {
attachBaseContext(context);
mFragments.attachHost(null /*parent*/);
mWindow = new PhoneWindow(this, window, activityConfigCallback);
//其它代码省略,详细可以参考Activity.java中的源代码
}
各位看官,关于Androd中View助手的例子咱们就介绍到这里,欲知后面还有什么例子,且听下回分解!
本文深入探讨了Android中PhoneWindow作为View助手的角色,解析其与WindowManager的交互及如何在Activity的onCreate()方法中被初始化。PhoneWindow是Activity与WindowManager之间的桥梁,负责管理Activity的Window。
259

被折叠的 条评论
为什么被折叠?



