WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL作用

本文介绍如何通过设置FLAG_NOT_TOUCH_MODAL标志位实现Android应用中自定义小窗口与底层交互的效果。即使小窗口处于前台,也能确保点击未被遮挡的区域时事件传递给底层界面。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Window flag: Even when this window is focusable (its {@link #FLAG_NOT_FOCUSABLE is not set), 
allow any pointer events outside of the window to be sent to the windows behind it.
即使在该window在可获得焦点情况下,仍然把该window之外的任何event发送到该window之后的其他window.

在一个Activity中弹出一个使用WindowManager创建的自定义小窗口,

params.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;

小窗口设置FLAG_NOT_TOUCH_MODAL,可以在弹出小窗口的同时,点击Activity中未被覆盖的按钮等。

package com.example.myapplicationdemo4.listView10; import android.content.Context; import android.graphics.PixelFormat; import android.os.Build; import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; import android.view.WindowManager; import android.widget.TextView; import com.example.myapplicationdemo4.R; public class FloatingWindowManager { private Context context; private WindowManager windowManager; private View floatingView; // 关键:保存悬浮窗View的引用 private WindowManager.LayoutParams params; public FloatingWindowManager(Context context) { this.context = context.getApplicationContext(); windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); } // 创建悬浮窗 public void createFloatingWindow() { WindowManager.LayoutParams params = new WindowManager.LayoutParams( WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT, Build.VERSION.SDK_INT >= Build.VERSION_CODES.O ? WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY : WindowManager.LayoutParams.TYPE_PHONE, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN, PixelFormat.TRANSLUCENT); params.gravity = Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL; params.x = 0; params.y = 0; // 关键:Inflate布局并保存引用 floatingView = LayoutInflater.from(context).inflate(R.layout.floating_layout, null); // 初始内容 TextView tvContent = floatingView.findViewById(R.id.tvContent); tvContent.setText("初始内容"); windowManager.addView(floatingView, params); } // 更新悬浮窗内容(核心方法) public void updateContent(String newText) { if (floatingView == null) return; // 通过保存的View引用直接修改内容 TextView tvContent = floatingView.findViewById(R.id.tvContent); tvContent.setText(newText); } // 移除悬浮窗 public void removeFloatingWindow() { if (floatingView != null) { windowManager.removeView(floatingView); floatingView = null; } } } 请帮我修改一下这个代码,来实现这个悬浮窗点击穿透到悬浮窗下层的界面
07-10
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值