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

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

  • Android  中view的显示视图么?
  • Activity,window,View的关系是什么?
  • LayOutInflater 填充是什么?
  • LayOutInflater 具体怎么做?

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

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

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

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

首先,我们找到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,
Object lastNonConfigurationInstance,
HashMap<String,Object> lastNonConfigurationChildInstances,
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;
}

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

public static Window makeNewWindow(Context context) {
return sPolicy.makeNewWindow(context);
}

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

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

    public PhoneWindow(Context context) {
super(context);
mLayoutInflater = LayoutInflater.from(context);
}

发现他是用一个layoutflater从布局文件中填充一个相应的界面,通过这一大篇源代码证明,我们看到window,activity,和view的关系是这个样子的:
activity中new出一个window,而window通过这个phonewindow子类来获取一个子类。这是一种典型composite模式。一个经典的应用,希望对大家带来帮助。

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

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

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

这就是我的理解。

好好学习,天天向上。

android 中的 window,view,activity具体关系的更多相关文章

  1. Android中退出多个Activity的两个经典方法

    这里介绍两种方法:一种把每个activity记住,然后逐一干掉:另一种思路是使用广播. 方法一.用list保存activity实例,然后逐一干掉 上代码: import java.util.Linke ...

  2. Android中的this、Activity、Context等

    Android中的this.Activity.Context.Application等虽然有相似之处,但是不能乱用,每一个都有自己的特点.用的时候不能太随意了. 避免context相关的内存泄露,注意 ...

  3. android中正确保存view的状态

    英文原文: http://trickyandroid.com/saving-android-view-state-correctly/ 转载此译文须注明出处. 今天我们聊一聊安卓中保存和恢复view状 ...

  4. Android中fragment之间和Activity的传值、切换

    功能介绍:通过一个activity下方的三个按钮,分别是发送消息(sendButton).聊天记录(chatButton).常用语(commonButton).当单击按钮是,来切换上方的fragmen ...

  5. Android中多个调用Activity的问题

    <application android:allowBackup="true" android:icon="@drawable/ic_launcher" ...

  6. android中使用Intent在activity之间传递数据

    android中intent传递数据的简单使用: 1.使用intent传递数据: 首先将需要传递的数据放入到intent中 Intent intent = new Intent(MainActivit ...

  7. android中activity,window,view之间的关系

    activity:控制单元 window:承载模型 view:显示视图 几个小tip: 1.一个 Activity 构造的时候一定会构造一个 Window(PhoneWindow),并且只有一个 2. ...

  8. Android中使用自定义View实现下载进度的显示

    一般有下载功能的应用都会有这样一个场景,需要一个图标来标识不同的状态.之前在公司的项目中写过一个,今天抽空来整理一下. 一般下载都会有这么几种状态:未开始.等待.正在下载.下载结束,当然有时候会有下载 ...

  9. Android中onTouch与onClick事件的关系

    这几天遇到点关于Android的触摸事件相关的,还跟onClick有关.暂且记下: LinearLayout分别设置了onTouchListener,onClickListener,onLongCli ...

随机推荐

  1. 机器学习之路: python 支持向量机 LinearSVC 手写字体识别

    使用python3 学习sklearn中支持向量机api的使用 可以来到我的git下载源代码:https://github.com/linyi0604/MachineLearning # 导入手写字体 ...

  2. 拉格朗日乘子法以及KKT条件

    拉格朗日乘子法是一种优化算法,主要用来解决约束优化问题.他的主要思想是通过引入拉格朗日乘子来将含有n个变量和k个约束条件的约束优化问题转化为含有n+k个变量的无约束优化问题. 其中,利用拉格朗日乘子法 ...

  3. BZOJ 2115: [Wc2011] Xor 线性基 dfs

    https://www.lydsy.com/JudgeOnline/problem.php?id=2115 每一条从1到n的道路都可以表示为一条从1到n的道路异或若干个环的异或值. 那么把全部的环丢到 ...

  4. 浅析SDWebImage

    浅析SDWebImage 在日常的开发过程中,如果去优雅的访问网络的图片并去管理每个工程必须要面对的问题,如果想要在工程里面提供易用.简洁.方便管理的解决方案还是很有挑战的,毕竟还要兼顾图片文件的缓存 ...

  5. pear中几个实用的xml代码库

    1.XML_Beautifier 用于将一段排版凌乱的XML文档美化 <?php require_once "XML/Beautifier.php"; $fmt = new ...

  6. Codeforces Round #359 (Div. 2) A. Free Ice Cream 水题

    A. Free Ice Cream 题目连接: http://www.codeforces.com/contest/686/problem/A Description After their adve ...

  7. An ac a day,keep wa away

    zoj 初学者题: 1001 1037 1048 1049 1051 1067 1115 1151 1201 1205 1216 1240 1241 1242 1251 1292 1331 1334 ...

  8. IO流-递归遍历目录下指定后缀名结尾的文件名称

    /* *自定义遍历目录下指定后缀名结尾文件的名称的方法: * * param file:指定目录 name:指定后缀名 */ 1 public static void FileName(File fi ...

  9. 字符串转码【String.getBytes()和new String()】

    在Java中,String.getBytes(String decode)方法会根据指定的decode编码返回某字符串在该编码下的byte数组表示,如 byte[] b_gbk = "中&q ...

  10. 小程序获取openid unionid session_key

    .wxml <button bindtap="paytap">授权</button> .js Page({ paytap: function () { va ...