Android的WindowManager解析
16lz
2021-01-23
#定义
开发者官网的定义
The interface that apps use to talk to the window manager.
Use Context.getSystemService(Context.WINDOW_SERVICE) to get one of these.
#WindowManager介绍
Android的WindowManager是一个系统的服务,这是负责管理z-ordered列表窗口,以及它们是如何被放在屏幕上。另外,当打开或关闭应用程序或旋转屏幕,它会自动执行窗口转换和动画。
每个Activity都有一个window,用于在屏幕上显示其内容。当你在Activity调用setContentView()方法,实际上是添加到了Activity的默认window上。默认的window充满整个屏幕,这样你的Activity遮盖其他的window。所以通常你不需要担心Windows 。你只是创建一个Activity,Android会为你做剩下的。
如果你想做一些不寻常的东西像创建浮动窗口并且不要填满屏幕。如果您想创建一个在其他应用程序前面可见的浮动窗口,则不能使用Activity,因为当另一个应用程序到达前台时,您的Activity将停止,其窗口将被隐藏或销毁。相反,您需要从后台服务显示窗口。
#浮动窗口的例子
WindowManager.LayoutParams p = new WindowManager.LayoutParams( // Shrink the window to wrap the content rather than filling the screen WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT, // Display it on top of other application windows, but only for the current user WindowManager.LayoutParams.TYPE_SYSTEM_ALERT, // Don't let it grab the input focus WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, // Make the underlying application window visible through any transparent parts PixelFormat.TRANSLUCENT);// Define the position of the window within the screenp.gravity = Gravity.TOP | Gravity.RIGHT;p.x = 0;p.y = 100;WindowManager windowManager = (WindowManager)getSystemService(WINDOW_SERVICE);windowManager.addView(myView, p);
##需要添加权限##
uses-permission android:name=“android.permission.SYSTEM_ALERT_WINDOW”/
更多相关文章
- Android 屏幕设置
- android之应用程序退到android桌面的实现
- Android dp方式的屏幕适配工具使用(Android Studio插件方式)
- android:configChanges 屏幕横竖屏切换
- android的TableLayout布局界面元素填满整个屏幕
- android 笔记 --- Android应用程序的权限列表
- 2、创建android应用程序
- Android 屏幕截图并用WindowManager显示
- android屏幕和键盘显示问题