一文了解 Window 层级顺序

App开发者的不知有没有发现,StatusBar 一直是盖在App 上面,不管是修改颜色,或者是写悬浮框,都无法盖住StatusBar。framework 开发,会出现一些定制,如盖住 StatusBar,不了解的可能用错,出现一些不必要的bug,官方文档也没有列出 Window 层级的规则。
所以希望通过下文给大家分享,Android 是如何制定显示层级规则的。

大概说下 Window 在 Android 中的概念

其实也可以好理解,和经常使用 Windows 操作系统一样,打开一个应用,出现界面,我们可以理解出现了一个窗口,所以 Window ≠ View

一个Activity 可以理解 对应一个 Window,理解源码的同学知道: ViewRootImpl 是对应一个 Window。

怎么看 Window 呢?

adb shell dumpsys window

抽取了几个典型的Window如下:

Window #2 Window{911875c u0 NavigationBar0}://导航栏
  ty=NAVIGATION_BAR
  isOnScreen=true
  isVisible=true
Window #4 Window{bf1a956 u0 StatusBar}://状态栏
  ty=STATUS_BAR
  isOnScreen=true
  isVisible=true
Window #11 Window{d377ae1 u0 InputMethod}://输入法,不显示
  ty=INPUT_METHOD
  isOnScreen=false
  isVisible=false
Window #12 Window{e190206 u0 com.android.settings/com.android.settings.Settings}://打开 App activity
  ty=BASE_APPLICATION
  isOnScreen=true
  isVisible=true
Window #16 Window{abcabb9 u0 com.android.systemui.ImageWallpaper}://壁纸
  ty=WALLPAPER
  isOnScreen=false
  isVisible=false

一般手机都会存在以上 Window,层级顺序从高 -> 低。

显示PopWindow

 Window #11 Window{513f711 u0 PopupWindow:3e4bfb}:
   ty=APPLICATION_SUB_PANEL
   isOnScreen=true
   sVisible=true

显示Dialog

Window #11 Window{a08f90b ...}:
  ty=APPLICATION
  isOnScreen=true
  isVisible=true

不难看出,Window 层级与 ty 有关系的,tytype 的简写。

Window 的分类

Application Window: 应用程序窗口

type 取值范围 [1,99]

/**
 * Start of window types that represent normal application windows.
 */
public static final int FIRST_APPLICATION_WINDOW = 1;
// activity 会使用 此 type
public static final int TYPE_BASE_APPLICATION   = 1;
// dialog 会使用 此 type
public static final int TYPE_APPLICATION        = 2;
// 冷启动会显示的 Window,真正启动页面显示之前的画面
public static final int TYPE_APPLICATION_STARTING = 3;
// 没玩过
public static final int TYPE_DRAWN_APPLICATION = 4;
/**
 * End of types of application windows.
 */
public static final int LAST_APPLICATION_WINDOW = 99;

Sub Window: 子窗口

子窗口:顾名思义,对应有主窗口。子窗口需要附在主窗口上,如PopWindow

type 取值范围 [1000,1999]

/**
 * 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_WINDOW = 1000;
public static final int TYPE_APPLICATION_PANEL = FIRST_SUB_WINDOW;
public static final int TYPE_APPLICATION_MEDIA = FIRST_SUB_WINDOW + 1;
public static final int TYPE_APPLICATION_SUB_PANEL = FIRST_SUB_WINDOW + 2;
public static final int TYPE_APPLICATION_ATTACHED_DIALOG = FIRST_SUB_WINDOW + 3;
public static final int TYPE_APPLICATION_MEDIA_OVERLAY  
  • 5
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值