首页 > 系统 > Android > 正文

Android的WindowManager解析

2019-11-09 16:44:18
字体:
来源:转载
供稿:网友

定义

开发者官网的定义 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”/

我的微信二维码如下

这里写图片描述

微信订阅号二维码如下:

这里写图片描述


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表