Android窗口类型

WindowManager会根据窗口的类型来决定Z-Order,通过Z-Order也就决定了窗口从上到下的显示顺序。下表个表明了窗口类型与Z-Order的关系。表中layer值越大表示窗口越靠上


System

NO. WINDOW TYPE 描述 LAYER 是否HIDE
1 TYPE_UNIVERSE_BACKGROUND Behind the universe of the real windows.
In multiuser systems shows on all users' windows.
1 hide
2 TYPE_PRIVATE_PRESENTATION Window for Presentation on top of private virtual display. 2
3 TYPE_WALLPAPER wallpaper window, placed behind any window that wants
 to sit on top of the wallpaper. In multiuser systems shows only on the owning user's window.wallpaper is at the bottom, though the window manager may move it.
2
4 TYPE_PHONE These are non-application windows providing user interaction with the phone (in particular incoming calls).These windows are normally placed above all applications, but behind the status bar.In multiuser systems shows on all users' windows. 3
5 TYPE_SEARCH_BAR the search bar.  There can be only one search bar window; it is placed at the top of the screen.
In multiuser systems shows on all users' windows.
4
6 TYPE_RECENTS_OVERLAY  Same layer as {@link #TYPE_SYSTEM_DIALOG} but only appears on one user's screen.In multiuser systems shows on all users' windows. 5 hide
7 TYPE_SYSTEM_DIALOG panel that slides out from the status bar In multiuser systems shows on all users' windows. 5
8 TYPE_TOAST transient notifications. In multiuser systems shows only on the owning user's window.toasts and the plugged-in battery thing 6
9 TYPE_PRIORITY_PHONE priority phone UI, which needs to be displayed even if the keyguard is active.  These windows must not take input focus, or they will interfere with the keyguard. In multiuser systems shows on all users' windows.SIM errors and unlock.  Not sure if this really should be in a high layer. 7
10 TYPE_DREAM Dreams (screen saver) window, just above keyguard. In multiuser systems shows only on the owning user's window.used for Dreams (screensavers with TYPE_DREAM windows) 8 hide
11 TYPE_SYSTEM_ALERT system window, such as low power alert. These windows are always on top of application windows. In multiuser systems shows only on the owning user's window.like the ANR / app crashed dialogs 9
12 TYPE_INPUT_METHOD internal input methods windows, which appear above the normal UI.  Application windows may be resized or panned to keep the input focus visible while this window is displayed. In multiuser systems shows only on the owning user's window. on-screen keyboards and other such input method user interfaces go here. 10
13 TYPE_INPUT_METHOD_DIALOG internal input methods dialog windows, which appear above the current input method window. In multiuser systems shows only on the owning user's window.on-screen keyboards and other such input method user interfaces go here. 11
14 TYPE_KEYGUARD_SCRIM keyguard scrim window. Shows if keyguard needs to be restarted.In multiuser systems shows on all users' windows. the safety window that shows behind keyguard while keyguard is starting 12 hide
15 TYPE_KEYGUARD keyguard window.In multiuser systems shows on all users' windows. the keyguard; nothing on top of these can take focus, since they are responsible for power management when displayed. 13
16 TYPE_KEYGUARD_DIALOG dialogs that the keyguard shows In multiuser systems shows on all users' windows. 14
17 TYPE_STATUS_BAR_SUB_PANEL panel that slides out from under the status bar In multiuser systems shows on all users' windows. 15 hide
18 TYPE_STATUS_BAR the status bar.  There can be only one status bar window; it is placed at the top of the screen, and all other windows are shifted down so they are below it. In multiuser systems shows on all users' windows. 16
19 TYPE_STATUS_BAR_PANEL panel that slides out from over the status bar In multiuser systems shows on all users' windows. 17
20 TYPE_VOLUME_OVERLAY The volume level overlay/dialog shown when the user
changes the system volume. In multiuser systems shows on all users' windows.the on-screen volume indicator and controller shown when the user changes the device volume
18 hide
21 TYPE_SYSTEM_OVERLAY system overlay windows, which need to be displayed on top of everything else.  These windows must not take input focus, or they will interfere with the keyguard. In multiuser systems shows only on the owning user's window.the on-screen volume indicator and controller shown when the user changes the device volume 19
22 TYPE_NAVIGATION_BAR Navigation bar (when distinct from status bar) In multiuser systems shows on all users' windows.the navigation bar, if available, shows atop most things 20 hide
23 TYPE_NAVIGATION_BAR_PANEL Navigation bar panel (when navigation bar is distinct from status bar) In multiuser systems shows on all users' windows. some panels (e.g. search) need to show on top of the navigation bar 21 hide
24 TYPE_SYSTEM_ERROR internal system error windows, appear on top of everything they can.In multiuser systems shows only on the owning user's window.system-level error dialogs 22
25 TYPE_MAGNIFICATION_OVERLAY Magnification overlay window. Used to highlight the magnified portion of a display when accessibility magnification is enabled.In multiuser systems shows on all users' windows.used to highlight the magnified portion of a display 23 hide
26 TYPE_DISPLAY_OVERLAY Display overlay window.  Used to simulate secondary display devices.In multiuser systems shows on all users' windows.used to simulate secondary display devices 24 hide
27 TYPE_DRAG the drag-and-drop pseudowindow.  There is only one drag layer (at most), and it is placed on top of all other windows.In multiuser systems shows only on the owning user's window.the drag layer: input for drag-and-drop is associated with this window,which sits above all other focusable windows 25 hide
28 TYPE_SECURE_SYSTEM_OVERLAY secure system overlay windows, which need to be displayed on top of everything else.  These windows must not take input focus, or they will interfere with the keyguard.This is exactly like {@link #TYPE_SYSTEM_OVERLAY} except that only the system itself is allowed to create these overlays.  Applications cannotobtain permission to create secure system overlays.In multiuser systems shows only on the owning user's window. 26 hide
29 TYPE_BOOT_PROGRESS The boot progress dialog, goes on top of everything in the world.In multiuser systems shows on all users' windows. 27 hide
30 TYPE_POINTER (mouse) pointer In multiuser systems shows on all users' windows.the (mouse) pointer layer 28 hide
31 TYPE_HIDDEN_NAV_CONSUMER Fake window to consume touch events when the navigation
 bar is hidden. In multiuser systems shows on all users' windows.
29 hide

Application

NO. WINDOW TYPE 描述 LAYER 是否HIDE
1 FIRST_APPLICATION_WINDOW Start of window types that represent normal application windows. 2
2 TYPE_BASE_APPLICATION an application window that serves as the "base" window
 of the overall application; all other application windows will appear on top of it.In multiuser systems shows only on the owning user's window.
2
3 TYPE_APPLICATION a normal application window.  The {@link #token} must be an Activity token identifying who the window belongs to. In multiuser systems shows only on the owning user's window. 2
4 TYPE_APPLICATION_STARTING special application window that is displayed while the application is starting.  Not for use by applications themselves;this is used by the system to display something until the application can show its own windows.In multiuser systems shows on all users' windows. 2
5 LAST_APPLICATION_WINDOW End of types of application windows. 2

Sub Window

NO. WINDOW TYPE 描述 LAYER 是否HIDE
1 FIRST_SUB_WINDOW Start of types of sub-windows.  The {@link #token} of these windows must be set to the window they are attached to.  These types of windows are kept next to their attached window in Z-order, and their coordinate space is relative to their attached window. 1
2 TYPE_APPLICATION_PANEL a panel on top of an application window.  These windows
 appear on top of their attached window.
1
3 TYPE_APPLICATION_ATTACHED_DIALOG like {@link #TYPE_APPLICATION_PANEL}, but layout of the window happens as that of a top-level window, as a child of its container. 1
4 TYPE_APPLICATION_MEDIA window for showing media (such as video).  These windows are displayed behind their attached window. 1
5 TYPE_APPLICATION_SUB_PANEL a sub-panel on top of an application window.  These windows are displayed on top their attached window and any {@link #TYPE_APPLICATION_PANEL} panels. 2
6 TYPE_APPLICATION_MEDIA_OVERLAY window for showing overlays on top of media windows. These windows are displayed between TYPE_APPLICATION_MEDIA and the application window.  They should be translucent to be useful.  This is a big ugly hack so: -1 hide
Sub Window对应的主窗口为0

Media Sublayer(-2) < Media Overlay sublayer (-1) < Main Layer(0) < Attached Dialog (1) < Sub panel Sublayer (2)

子窗口与父窗口共用一个token。的父窗口一般是所在的Activity,比如surfaceView是一个子窗口,他的父窗口是所在activity所在的window


窗口类型与Layer转换的函数为windowTypeToLayerLw

/frameworks/base/policy/src/com/android/internal/policy/impl/PhoneWindowManager.java

public int windowTypeToLayerLw(int type) {
        if (type >= FIRST_APPLICATION_WINDOW && type <= LAST_APPLICATION_WINDOW) {
            return 2;
        }
        switch (type) {
        case TYPE_UNIVERSE_BACKGROUND:
            return 1;
        case TYPE_WALLPAPER:
            // wallpaper is at the bottom, though the window manager may move it.
            return 2;
        case TYPE_PHONE:
            return 3;
        case TYPE_SEARCH_BAR:
            return 4;
        case TYPE_RECENTS_OVERLAY:
        case TYPE_SYSTEM_DIALOG:
            return 5;
        case TYPE_TOAST:
            // toasts and the plugged-in battery thing
            return 6;
        case TYPE_PRIORITY_PHONE:
            // SIM errors and unlock.  Not sure if this really should be in a high layer.
            return 7;
        case TYPE_DREAM:
            // used for Dreams (screensavers with TYPE_DREAM windows)
            return 8;
        case TYPE_SYSTEM_ALERT:
            // like the ANR / app crashed dialogs
            return 9;
        case TYPE_INPUT_METHOD:
            // on-screen keyboards and other such input method user interfaces go here.
            return 10;
        case TYPE_INPUT_METHOD_DIALOG:
            // on-screen keyboards and other such input method user interfaces go here.
            return 11;
        case TYPE_KEYGUARD:
            // the keyguard; nothing on top of these can take focus, since they are
            // responsible for power management when displayed.
            return 12;
        case TYPE_KEYGUARD_DIALOG:
            return 13;
        case TYPE_STATUS_BAR_SUB_PANEL:
            return 14;
        case TYPE_STATUS_BAR:
            return 15;
        case TYPE_STATUS_BAR_PANEL:
            return 16;
        case TYPE_VOLUME_OVERLAY:
            // the on-screen volume indicator and controller shown when the user
            // changes the device volume
            return 17;
        case TYPE_SYSTEM_OVERLAY:
            // the on-screen volume indicator and controller shown when the user
            // changes the device volume
            return 18;
        case TYPE_NAVIGATION_BAR:
            // the navigation bar, if available, shows atop most things
            return 19;
        case TYPE_NAVIGATION_BAR_PANEL:
            // some panels (e.g. search) need to show on top of the navigation bar
            return 20;
        case TYPE_SYSTEM_ERROR:
            // system-level error dialogs
            return 21;
        case TYPE_MAGNIFICATION_OVERLAY:
            // used to highlight the magnified portion of a display
            return 22;
        case TYPE_DISPLAY_OVERLAY:
            // used to simulate secondary display devices
            return 23;
        case TYPE_DRAG:
            // the drag layer: input for drag-and-drop is associated with this window,
            // which sits above all other focusable windows
            return 24;
        case TYPE_SECURE_SYSTEM_OVERLAY:
            return 25;
        case TYPE_BOOT_PROGRESS:
            return 26;
        case TYPE_POINTER:
            // the (mouse) pointer layer
            return 27;
        case TYPE_HIDDEN_NAV_CONSUMER:
            return 28;
        }
        Log.e(TAG, "Unknown window type: " + type);
        return 2;
    }


    public int subWindowTypeToLayerLw(int type) {
        switch (type) {
        case TYPE_APPLICATION_PANEL:
        case TYPE_APPLICATION_ATTACHED_DIALOG:
            return APPLICATION_PANEL_SUBLAYER;
        case TYPE_APPLICATION_MEDIA:
            return APPLICATION_MEDIA_SUBLAYER;
        case TYPE_APPLICATION_MEDIA_OVERLAY:
            return APPLICATION_MEDIA_OVERLAY_SUBLAYER;
        case TYPE_APPLICATION_SUB_PANEL:
            return APPLICATION_SUB_PANEL_SUBLAYER;
        }
        Log.e(TAG, "Unknown sub-window type: " + type);
        return 0;
    }

1).  Window类的基本属性如下:

          FEATURE_OPTIONS_PANEL = 0;                      可选的Panel标志(默认使能)
          FEATURE_NO_TITLE = 1;                                 无标题栏
          FEATURE_PROGRESS = 2;                                 标题栏上显示加载进度,如webview加载网页时(条状进度条)
          FEATURE_LEFT_ICON = 3;                                标题栏左侧显示一个图标
          FEATURE_RIGHT_ICON = 4;                              标题栏右侧显示一个图标
          FEATURE_INDETERMINATE_PROGRESS = 5;   不确定的进度(圆圈状等待图标)
          FEATURE_CONTEXT_MENU = 6;                        Menu菜单(默认使能)
          FEATURE_CUSTOM_TITLE = 7;                          自定义标题栏,该属性不能与其他标题栏属性合用
          FEATURE_ACTION_BAR = 8;                               ActionBar属性,ActionBar显示的,TitleBar就没有啦                                                             

          FEATURE_ACTION_BAR_OVERLAY = 9;            覆盖在内容之上的ActionBar

          FEATURE_ACTION_MODE_OVERLAY = 10;        AcitionBar的覆盖在内容的模式

          FEATURE_OPENGL = 8;                                       若开启OpenGL,那么2D将由OpenGL处理(OpenGL中2D是3D的子集)
          PROGRESS_VISIBILITY_ON = -1;                         进度条可见
          PROGRESS_VISIBILITY_OFF = -2;                       进度条不可见
          PROGRESS_INDETERMINATE_ON = -3;              开启不确定模式
          PROGRESS_INDETERMINATE_OFF = -4;             关闭不确定模式
          PROGRESS_START = 0;                                        第一进度条的最小值
          PROGRESS_END = 10000;                                     第一进度条的最大值
          PROGRESS_SECONDARY_START = 20000;          第二进度条的最小值
          PROGRESS_SECONDARY_END = 30000;              第二进度条的最大值

       经常使用到的:

    1、隐藏标题栏

        requestWindowFeature(Window.FEATURE_NO_TITLE);

    2、在标题栏显示进度条

        requestWindowFeature(Window.FEATURE_PROGRESS);

        setContentView(R.layout.progressbar_1);

        setProgressBarVisibility(true);

        final ProgressBar progressHorizontal = (ProgressBar)findViewById(R.id.progress_horizontal);

        setProgress(progressHorizontal.getProgress() * 100);

        setSecondaryProgress(progressHorizontal.getSecondaryProgress()* 100);

    3、使用自定义标题栏

        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);

        setContentView(R.layout.xxx);

        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.my_title_bar);

    4、清除标题栏内容,而区域保留

        ((ViewGroup)getWindow().findViewById(com.android.internal.R.id.title_container)).removeAllViews();

    5、隐藏标题栏

        ((ViewGroup)getWindow(). findViewById(com.android.internal.R.id.title_container)).setVisibility(View.GONE);

    6、显示标题栏

        ((ViewGroup)getWindow(). findViewById(com.android.internal.R.id.title_container)).setVisibility(View.VISIBLE);

    注意事项

    (1) requestWindowFeature()要在setContentView()之前调用;

    (2) 设置各种Feature,是具有排它性的,一旦设置,后续不可更改为别的类型;

    (3)当使用TabHost(由ActivityGroup派生)时,各个Tab里的Activity,要么都是NO_TITLE,要么都是CUSTOM_TITLE,无法分别进行设置

    ID_ANDROID_CONTENT = com.android.internal.R.id.content;  就是显示内容的id。


    2). Window类的内部类接口Callback包含一系列dispatchXxxx函数和一系列onXxxx函数,用于处理UI事件

        public boolean dispatchKeyEvent(KeyEvent event);

        public boolean dispatchKeyShortcutEvent(KeyEvent event);

        public boolean dispatchTouchEvent(MotionEvent event);

        public boolean dispatchTrackballEvent(MotionEvent event);

        public boolean dispatchGenericMotionEvent(MotionEvent event);                            移动事件

        public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event);

        public View onCreatePanelView(int featureId);

        public boolean onCreatePanelMenu(int featureId, Menu menu);

        public boolean onPreparePanel(int featureId, View view, Menu menu);

        public boolean onMenuOpened(int featureId, Menu menu);                                      用户选择菜单项。

        public boolean onMenuItemSelected(int featureId, MenuItem item);                          用户选择菜单项。

        public void onWindowAttributesChanged(WindowManager.LayoutParams attrs);        当前窗口属性改变。

        public void onContentChanged();                                  窗口的view发生改变。

        public void onWindowFocusChanged(boolean hasFocus);窗口的焦点发生变换

        public void onAttachedToWindow();                              窗口附加到窗口管理器上。

        public void onDetachedFromWindow();                          窗口从窗口管理器上摘除。

        public void onPanelClosed(int featureId, Menu menu);

        public boolean onSearchRequested();                       当用户按下了search按钮。如果启动了搜索界面则返回true,如果不能执行则返回false。

        public ActionMode onWindowStartingActionMode(ActionMode.Callback callback);

        public void onActionModeStarted(ActionMode mode);

        public void onActionModeFinished(ActionMode mode);

你可能感兴趣的:(Android)