Window你真的了解吗-[Android_YangKe]

Android_YangKe.jpg
目录
  • Window是什么,与PhoneWindow的关系亲密吗?
  • PhoneWindow源码分析
1、Window是什么,与PhoneWindow的关系亲密吗?
Android_YangKe.png
2、PhoneWindow源码分析
/**
 * Abstract base class for a top-level window look and behavior policy.  An
 * instance of this class should be used as the top-level view added to the
 * window manager. It provides standard UI policies such as a background, title
 * area, default key processing, etc.
 *
 * 

The only existing implementation of this abstract class is * android.view.PhoneWindow, which you should instantiate when needing a * Window. */ public abstract class Window { ... ... }

以上源码中我可以获取到以下信息:

  • Window是一个抽象类,约束了一个窗口的外观和行为;
  • 提供了标准的UI策略,像背景,标题区域;
  • Window只有一个实现类,名为PhoneWindow;

onCreate函数我们天天用,setContentView肯定很熟悉,深入源码阅读,内部其实就是直接调用PhoneWindow的setContentView函数。

//package android.app;
final void attach(Context context, ... .., ActivityConfigCallback callBack) {
    ... ...省略n行
    //构建mWindow对象
    mWindow = new PhoneWindow(this, window, activityConfigCallback);
}

public Window getWindow() {
    //attach 函数中初始了mWindow对象,此对象的类型为PhoneWindow
    return mWindow;
}

public void setContentView(@LayoutRes int layoutResID) {
    //获取Window对象,其实就是获取PhoneWindow对象
    getWindow().setContentView(layoutResID);
    //初始化ActionBar
    initWindowDecorActionBar();
}

总结:

  • 初始化mWindow对象,对象的类型为PhoneWindow;
  • 调用PhoneWindow的setContentView函数;
  • 初始化Window的ActionBar;
  • Activity只负责生命周期函数的调用,具体承载视图配置交给了PhoneWindow;

initWindowDecorActionBar函数:

/**
  * Creates a new ActionBar, locates the inflated ActionBarView,
  * initializes the ActionBar with the view, and sets mActionBar.
  */
  //创建WindowDecorActionBar实例,填充属性
  private void initWindowDecorActionBar() { 
      ... ...
      //构建WindowDecorActionBar对象
      mActionBar = new WindowDecorActionBar(this);
     //初始化WindowDecorActionBar属性
      mActionBar.setDefaultDisplayHomeAsUpEnabled(mEnableDefaultActionBarUp);
      mWindow.setDefaultIcon(mActivityInfo.getIconResource());
      mWindow.setDefaultLogo(mActivityInfo.getLogoResource());
}

总结:

  • 构建WindowDecorActionBar实例;
  • 配置WindowDecorActionBar属性;
  • 为PhoneWindow设置图标,其本质是设置依附于此窗口的View;

getWindow().setContentView函数:

public void setContentView(int layoutResID) {
    if (mContentParent == null) {
        installDecor();//初始化Window上系统封装的View,ViewGroup
    } 
   ... ...
    //解析xml文件,填充View
    mLayoutInflater.inflate(layoutResID, mContentParent);
}

总结:

  • 初始化Window表面系统封装的View,ViewGroup;
  • 借助LayoutInflater填充视图

installDecor函数:
前言:

// This is the top-level view of the window, containing the window decor.
//继承自FragmeLayout,Window上顶级的视图载体
private DecorView mDecor;
public class DecorView extends FrameLayout implements  { 
    ... 省略n行...
}

// This is the view in which the window contents are placed. It is either
// mDecor itself, or a child of mDecor where the contents go.
// 用于真正包裹视图的ViewGroup
ViewGroup mContentParent;

private void installDecor() {
    //构建mDecor对象
    mDecor = generateDecor(-1);
    mDecor.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
    mDecor.setIsRootNamespace(true);
    ... ....
    //构建mContentParent对象,
    mContentParent = generateLayout(mDecor);
}

总结:

  • 初始化mDecor对象,其为ViewGroup的子类,是Window上顶级的视图载体;
  • 初始化mContentParent,此对象为窗口上真正视图的承载,内部对输入法的模式,窗口的样式进行了配置;
    • 全屏窗口样式
    • 无标题窗口样式

mLayoutInflater.inflate(layoutResID, mContentParent)函数:

/**
  * Inflate a new view hierarchy from the specified xml resource.
 *  Throws {@link InflateException} if there is an error.
  */
//从指定的xml资源文件填充视图,遇到错误会抛出InflateException异常
public View inflate(@LayoutRes int resource, @Nullable ViewGroup root) {
    return inflate(resource, root, root != null);
}
//填充视图,内部借助XmlPullParser
mLayoutInflater.inflate(layoutResID, mContentParent);

总结:从指定的xml文件解析填充视图,内部借助XmlPullParser。

整体汇总:
Window是一个抽象类,其唯一的实现类的为PhoneWindow。Window我们可以抽象的理解为一个窗口主要用来约束窗口的外观及行为。比如:页面样式,输入法的模式,是Activity与View进行交互的一个桥梁。说到这里我们就要提一下Activity、Window、View。

View

具体的视图载体,可以是TextView,ImageView,Button等,负责各种各种样的视图,可以是一个,或者一组视图。

Window

Window是一个抽象的概念,我们简单理解为一个窗口或者是页面。它是视图的底层载体,不对具体的视图做操作,但没有Window我们的视图就失去了地基,无法盖楼。

Activity

Android中的四大组件,一般对应一个或者多个页面,有自己的生命周期,启动模式,接收屏幕上的事件等。

Android_YangKe.png

喜欢有帮助的话: 双击、评论、转发,动一动你的小手让更多的人知道!关注 Android_YangKe

你可能感兴趣的:(Window你真的了解吗-[Android_YangKe])