Android之Window

window表示的是一个抽象窗口类,该类只是一个抽象窗口类,其具体的唯一实现类是PhoneWindow类。Window对象的获取通过在Activity中调用getWindow()方法获取到Window对象;该类定义了一个CallBack接口,用于处理用户的消息数据,该接口的方法有:dispatchKeyEvent,dispatchTouchEvent等,
他设置了一组通用的窗口操作api。比如setContentView方法,实际上Activity中的setContentView方法是调用的Activity内部的Window中的setContentView方法来实现的,除了这个方法之外,还有findViewById()方法也是调用的Window方法的findViewById()方法。
在Window类中,定义了一组常量值,用于表示窗口的样式:常量值如下所示:

  /** Flag for the "options panel" feature.  This is enabled by default.表示有菜单项,默认有菜单项 */
    public static final int FEATURE_OPTIONS_PANEL = 0;
    /** Flag for the "no title" feature, turning off the title at the top
     *  of the screen.表示无标题栏,也就是应用程序上面没有标题栏 */
    public static final int FEATURE_NO_TITLE = 1;
    /** Flag for the progress indicator feature进度条 */
    public static final int FEATURE_PROGRESS = 2;
    /** Flag for having an icon on the left side of the title bar在标题栏的左侧有图标 */
    public static final int FEATURE_LEFT_ICON = 3;
    /** Flag for having an icon on the right side of the title bar在标题栏的右侧有图标 */
    public static final int FEATURE_RIGHT_ICON = 4;
    /** Flag for indeterminate progress 不确定的进度条,比如圆圈状的等待条*/
    public static final int FEATURE_INDETERMINATE_PROGRESS = 5;
    /** Flag for the context menu.  This is enabled by default.上下文菜单 */
    public static final int FEATURE_CONTEXT_MENU = 6;
    /** Flag for custom title. You cannot combine this feature with other title features. 自定义的标题栏,该属性不能和其他属性合用*/
    public static final int FEATURE_CUSTOM_TITLE = 7;
    /**
     * Flag for enabling the Action Bar.
     * This is enabled by default for some devices. The Action Bar
     * replaces the title bar and provides an alternate location
     * for an on-screen menu button on some devices.
     */
    public static final int FEATURE_ACTION_BAR = 8;
      public static final int FEATURE_ACTION_BAR_OVERLAY = 9;
    /**
     * Flag for specifying the behavior of action modes when an Action Bar is not present.
     * If overlay is enabled, the action mode UI will be allowed to cover existing window content.
     */
    public static final int FEATURE_ACTION_MODE_OVERLAY = 10;

    /**
     * Max value used as a feature ID
     * @hide
     */
    public static final int FEATURE_MAX = FEATURE_ACTION_MODE_OVERLAY;

    /** Flag for setting the progress bar‘s visibility to VISIBLE */
    public static final int PROGRESS_VISIBILITY_ON = -1;
    /** Flag for setting the progress bar‘s visibility to GONE */
    public static final int PROGRESS_VISIBILITY_OFF = -2;
    /** Flag for setting the progress bar‘s indeterminate mode on */
    public static final int PROGRESS_INDETERMINATE_ON = -3;
    /** Flag for setting the progress bar‘s indeterminate mode off */
    public static final int PROGRESS_INDETERMINATE_OFF = -4;
    /** Starting value for the (primary) progress */
    public static final int PROGRESS_START = 0;
    /** Ending value for the (primary) progress */
    public static final int PROGRESS_END = 10000;
    /** Lowest possible value for the secondary progress */
    public static final int PROGRESS_SECONDARY_START = 20000;
    /** Highest possible value for the secondary progress */

实际上我们熟知的Activity与Widnow类联系很紧密,每个Activity中都有自己的Window类。在每个Activity类的内部都有一个私有变量private Window mWindow;该对象的赋值是在Activity的attach()方法中赋值的,通过mWindow = PolicyManager.makeNewWindow(this);这个语句来为对象赋值,而attach方法的调用是在ActivityThread中创建Activity对象时调用的。PolicyManager.makeNewWindow(this);该方法归根结底只是创建出一个PhoneWindow对象赋值给mWindow对象。另外Window类可以接受用户的消息处理的,而Activity通过实现Window类的CallBack接口从而也能够处理用户的消息。
window对象的一些常用方法:
setContentView(),activity中setContentView就是调用该方法
findViewById(),activity中findViewById就是调用该方法
getLayoutInflater(),activity中getLayoutInflater就是调用该方法
getContext(),返回当前的上下文环境
getWindowManager()返回窗口管理器

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。