android windows区别,android Window,Activity,View的区别

通过讨论这个问题,我们能够见识到google是对面向对象模式的理解,能够理解android底层的一些调用。这也是一道很常见的面试题。

我们这篇文章就来解决这四个问题:

Android 中view的显示视图么?

Activity,window,View的关系是什么?

LayOutInflater 填充是什么?

LayOutInflater 具体怎么做?

首先,我们从activity开始说起,说起activity我们都要知道setcontentview和attach方法。setcontentview中的,主要用来填充相应的布局文件。而至于attach方法了,这个方法用的很少,但是很重要。

我们跟踪java的源代码了,我们清晰的看到这个activity实际上是调用phonewindow的setcontentview中的方法来进行界面的呈现。他的类图如下:

a4c26d1e5885305701be709a3d33442f.png

而phonewindow有初始化了一个ViewGroup对象,这个ViewGroup的子类,可以显示每个控件的视图,还有一个LayoutInflator使xml文件能够填充为相应的视图。他们的类图又是如下:

a4c26d1e5885305701be709a3d33442f.png

我们看看具体的源代码如下:

首先,我们找到activity中的attach方法,这是这个程序启动的时候最重要的方法。了,这是他的源代码:

a4c26d1e5885305701be709a3d33442f.png

final voidattach(Context context, ActivityThread aThread,

Instrumentation instr, IBinder token,intident,

Application application, Intent intent, ActivityInfo info,

CharSequence title, Activity parent, String id,

Object lastNonConfigurationInstance,

HashMaplastNonConfigurationChildInstances,

Configuration config) {

attachBaseContext(context);

mWindow= PolicyManager.makeNewWindow(this);

mWindow.setCallback(this);if (info.softInputMode !=WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED) {

mWindow.setSoftInputMode(info.softInputMode);

}

mUiThread=Thread.currentThread();

mMainThread=aThread;

mInstrumentation=instr;

mToken=token;

mIdent=ident;

mApplication=application;

mIntent=intent;

mComponent=intent.getComponent();

mActivityInfo=info;

mTitle=title;

mParent=parent;

mEmbeddedID=id;

mLastNonConfigurationInstance=lastNonConfigurationInstance;

mLastNonConfigurationChildInstances=lastNonConfigurationChildInstances;

mWindow.setWindowManager(null, mToken, mComponent.flattenToString());if (mParent != null) {

mWindow.setContainer(mParent.getWindow());

}

mWindowManager=mWindow.getWindowManager();

mCurrentConfig=config;

}

a4c26d1e5885305701be709a3d33442f.png

我们看到policymanager对象中有一个window对象,这个window就是现实的窗口了。他是一个抽象类,又是如何new出这个对象,我们继续关注。这就来到了policymanager的类中了。看makenewwindow这个方法:

public staticWindow makeNewWindow(Context context) {returnsPolicy.makeNewWindow(context);

}

他又是传递context调用ipolicy接口中的makeNewWindow方法,来返回一个window对象。继续深挖,我们来看makenewwindow这个方法真实的本来面目。

这个本身的makewindow是ipolicy的一个抽象方法,他是发包给policy实现的,这个policy是new出一个phonewindow对象。 这个phonewindow的类又是怎么样子了。关注源代码我们得知,他是继承与window这个基类的,饶了一大圈以后,发现这个makenewwindow确实是new出一个window的对象。

他构造函数是这个样子:

publicPhoneWindow(Context context) {super(context);

mLayoutInflater=LayoutInflater.from(context);

}

发现他是用一个layoutflater从布局文件中填充一个相应的界面,通过这一大篇源代码证明,我们看到window,activity,和view的关系是这个样子的:

activity中new出一个window,而window通过这个phonewindow子类来获取一个子类。这是一种典型composite模式。一个经典的应用,希望对大家带来帮助。

怎么添加窗口上了,是通过setcontentview方法,我们看到这个还是通过phonewindow的setcontentview方法,他的源代码如下:

a4c26d1e5885305701be709a3d33442f.png

public voidsetContentView(View view, ViewGroup.LayoutParams params) {if (mContentParent == null) {

installDecor();

}else{

mContentParent.removeAllViews();

}

mContentParent.addView(view, params);final Callback cb =getCallback();if (cb != null) {

cb.onContentChanged();

}

}

a4c26d1e5885305701be709a3d33442f.png

他是通过mcontentparent来添加视图,这更进一步揭示了window与view的关系。

这就是我的理解。

好好学习,天天向上。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值