Android WindowManager & Window属性 & 添加悬浮窗的示例代码

#.WindowManager

    Android中用来管理Window的接口类,具体实现类是WindowManagerImpl,而最终是交给WindowManagerService来真实实现相应功能。
##.基本API:

1.addView(View, WindowManager.LayoutParams);

添加Window,并把View作为Window的内容,WindowManager.LayoutParams配置了窗口的参数信息。

2.updateViewLayout(View, WindowManager.LayoutParams);

更新指定Window的配置参数

3.removeView(View);

移除View对应的Window。

#.Window的属性

Window的属性定义在WindowManager.LayoutParams类中。相关属性有很多种,与应用开发最密切的有四类,分别是 Type(Window的类型)、 Flag(Window的标志)、 SoftInputMode(软键盘相关模式)、位置大小相关属性。

##.Window的类型

对应WindowManager.LayoutParams.type,Window的类型同时会决定Window的显示层级Z-Order。
Window的类型有很多种,但整体分为三大类:
1)ApplicationWindow(应用程序窗口):一般位于最底层,Z-Order在1-99
2)Sub Window(子窗口):子窗口必须依附于特定的父Window,Z-Order在1000-1999
3)SystemWindow(系统窗口): 系统级窗口一般位于最顶层,不会被其他的window遮住(如Toast),Z-Order在2000-2999。如果要弹出自定义系统级窗口需要动态申请权限。

1.应用程序窗口

典型的应用程序窗口就是Activity中的窗口。 类型如下:
// 应用程序 Window 的开始值
public static final int FIRST_APPLICATION_WINDOW = 1;
// 应用程序 Window 的基础值
public static final int TYPE_BASE_APPLICATION = 1;
// 普通的应用程序
public static final int TYPE_APPLICATION = 2;
// 特殊的应用程序窗口,当程序可以显示 Window 之前使用这个 Window 来显示一些东西
public static final int TYPE_APPLICATION_STARTING = 3;
// TYPE_APPLICATION 的变体,在应用程序显示之前,WindowManager 会等待这个 Window 绘制完毕
public static final int TYPE_DRAWN_APPLICATION = 4;
// 应用程序 Window 的结束值
public static final int LAST_APPLICATION_WINDOW = 99;

注意:Activity和Dialog中的Window的类型都是TYPE_APPLICATION,Dialog不是子窗口,但Dialog必须附属于某个Activity,用这个Activity作为Context来创建。

2.子窗口

子窗口不能独立存在,必须依附于特定的父Window才行,例如PopupWindow中的Window(类型
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个简单的使用 Android 系统自带的 WindowManager 实现悬浮示例代码: 1. 创建一个新的 Activity,命名为 FloatingActivity,并在 AndroidManifest.xml 中注册。 2. 在 FloatingActivity 的 onCreate 方法中添加以下代码: ```java // 创建悬浮口布局参数 WindowManager.LayoutParams params = new WindowManager.LayoutParams(); // 设置悬浮口的位置、大小、透明度等属性 params.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY; params.format = PixelFormat.RGBA_8888; params.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE; params.gravity = Gravity.CENTER; params.width = WindowManager.LayoutParams.WRAP_CONTENT; params.height = WindowManager.LayoutParams.WRAP_CONTENT; params.alpha = 1.0f; // 创建悬浮口视图 LayoutInflater inflater = LayoutInflater.from(this); View floatingView = inflater.inflate(R.layout.floating_view, null); // 添加悬浮口视图 WindowManager windowManager = (WindowManager) getSystemService(WINDOW_SERVICE); windowManager.addView(floatingView, params); ``` 3. 在 res/layout 目录下创建一个名为 floating_view.xml 的布局文件,用于定义悬浮口的视图。 4. 在 floating_view.xml 中添加以下代码: ```xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#FFFFFF" android:padding="16dp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello, world!" /> </RelativeLayout> ``` 5. 运行程序,你将会看到一个显示 "Hello, world!" 的悬浮口在屏幕中央。 以上代码只是一个简单的示例,实际的悬浮口需要根据具体需求进行设计和实现。另外,由于 Android 8.0 以上版本对悬浮口的限制更加严格,需要特别注意权限和适配问题。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值