做系统层开发,往往会遇到UI显示图层的一些设置,比如一些界面可以直接被其他界面遮挡,有些界面只能被部分界面遮挡,甚至有些界面需要置顶不被任何界面遮挡;每个界面就是一个Window(窗口),是Window就受WindowManager管理,WindowManager中存在对应的LayoutParams(布局属性),通过设置这些属性可以控制Window的类型,Window的类型中自然就包含其显示图层的控制。
说到这儿我还想谈谈自己对Window的理解,顾名思义它就是一个窗口,这个窗口里面可以包含一些布局,布局中又可以包含一些View控件,整个组成一个可以展示出来供人交互的界面。Android的界面显示是一个三维空间,x,y轴控制需要展示的界面的大小,z轴控制界面显示的图层,这就存在界面显示优先级的说法了。
下面简单的介绍下Window的优先级的设置方法:
private Window mWindow;
mWindow = getWindow();
mWindow.setType(WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG); // 各种参数的优先级级别请参考下面Android源码
adb shell进去执行以下指令可以看到Window所在layer:
dumpsys -l :列出所有可以查看的类型
dumpsys window :列出所有正在运行的window相关参数,Surface: shown=true layer=161000 alpha=1.0 rect=(0.0,0.0) 1920.0 x 1.0参数有图层的大小
dumpsys SurfaceFlinger :界面显示的层叠关系会必现的比较直观
/**
* Start of window types that represent normal application windows.
*/
public static final int FIRST_APPLICATION_WINDOW = 1;
/**
* Window type: 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.
*/
public static final int TYPE_BASE_APPLICATION = 1;
/**
* Window type: 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.
*/
public static final int TYPE_APPLICATION = 2;
/**
* Window type: 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.
*/
public static final int TYPE_APPLICATION_STARTING = 3;
/**
* End of types of application windows.
*/
public static final int LAST_APPLICATION_WINDOW = 99;
/**
* 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.
*/
public static final int FIRST_SUB_WINDO