我尝试用谷歌搜索,但没有直接和/或明确的答案。
developer website的定义也不清楚:
The interface that apps use to talk to the window manager. Use
Context.getSystemService(Context.WINDOW_SERVICE)
to get one of these.
6 年级英语简单的人能解释一下它是什么吗?
我如何使用它来创建一个 float 对象,即使我从一个移动到另一个,也可以通过多个 activties 保留?
最佳答案
Android WindowManager 是一个系统服务,它负责管理窗口的 z 顺序列表、哪些窗口是可见的以及它们在屏幕上的布局方式。除其他外,它会在打开或关闭应用程序或旋转屏幕时自动执行窗口转换和动画。
每个 Activity 都有一个窗口,用于在屏幕上显示其内容。当您在 Activity 上调用 setContentView 时,它会将该 View 附加到 Activity 的默认窗口。默认窗口会填满屏幕,因此您的 Activity 窗口会隐藏任何其他 Activity ——WindowManager 将显示位于顶部的任何窗口。所以通常你不需要担心 windows - 你只需创建一个 Activity ,Android 会为你做剩下的事情。
但是如果你想做一些不寻常的事情,比如创建不填满屏幕的 float 窗口,你需要与 WindowManager 交互。如果要创建一个在其他应用程序面前可见的 float 窗口,则不能使用 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 screen
p.gravity = Gravity.TOP | Gravity.RIGHT;
p.x = 0;
p.y = 100;
WindowManager windowManager = (WindowManager)getSystemService(WINDOW_SERVICE);
windowManager.addView(myView, p);
为此,您需要向您的 AndroidManifest.xml 添加以下权限
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
关于android - 什么是android中的WindowManager?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19846541/