第8章 理解Window和WindowManager
WindowManager
通过WindowManager添加Window(悬浮窗)
mFloatingButton = new Button(this);
mFloatingButton.setText("click me");
mLayoutParams = new WindowManager.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 0, 0,
PixelFormat.TRANSPARENT);
mLayoutParams.flags = LayoutParams.FLAG_NOT_TOUCH_MODAL
| LayoutParams.FLAG_NOT_FOCUSABLE
| LayoutParams.FLAG_SHOW_WHEN_LOCKED;
mLayoutParams.type = LayoutParams.TYPE_SYSTEM_ERROR;
mLayoutParams.gravity = Gravity.LEFT | Gravity.TOP;
mLayoutParams.x = 100;
mLayoutParams.y = 300;
mFloatingButton.setOnTouchListener(this);
mWindowManager.addView(mFloatingButton, mLayoutParams);
flag:
FLAG_NOT_FOCUSABLE:Window不需要获取焦点,也不需要接收各种输入事件;
FLAG_NOT_TOUCH_MODEL:系统会将当前Window区域以为的单击事件传递给底层Window,当前Window内的单击事件则自己处理;
FLAG_SHOW_WHEN_LOCKED:Window显示在锁屏的界面上;
type:
Window的三种类型,应用Window(对应一个Activity),子Window(不能单独存在,需要附属在特定的父Window中),系统Window。
Window有层级概念,应用Window层级范围(1~99),子Window层级范围(1000~1999),系统Window层级范围(2000~2999);
常见的三大操作,添加View、更新View、删除View。
勿忘初衷