mui.plusReady(function() {
var WindowManager = plus.android.importClass("android.view.WindowManager");
var LayoutInflater = plus.android.importClass("android.view.LayoutInflater");
var Gravity = plus.android.importClass("android.view.Gravity");
var View = plus.android.importClass("android.view.View");
var WindowManagerLayoutParams = plus.android.importClass("android.view.WindowManager$LayoutParams");
var Context = plus.android.importClass("android.content.Context");
var Color = plus.android.importClass("android.graphics.Color");
var TextView = plus.android.importClass("android.widget.TextView");
// 获取应用上下文
var context = plus.android.runtimeMainActivity().getApplicationContext();
// 获取 WindowManager 实例
var windowManager = context.getSystemService(Context.WINDOW_SERVICE);
// 创建一个简单的 TextView 作为悬浮窗的内容
var textView = new TextView(context);
textView.setText("悬浮窗内容");
textView.setTextSize(20);
textView.setBackgroundColor(Color.parseColor("#80FF0000")); // 半透明红色背景
textView.setTextColor(Color.WHITE);
textView.setPadding(10, 10, 10, 10); // 内边距
// 创建 LayoutParams
var params = new WindowManagerLayoutParams(
WindowManagerLayoutParams.WRAP_CONTENT, // 宽度
WindowManagerLayoutParams.WRAP_CONTENT, // 高度
WindowManagerLayoutParams.TYPE_APPLICATION_OVERLAY, // 悬浮窗类型
WindowManagerLayoutParams.FLAG_NOT_FOCUSABLE | WindowManagerLayoutParams.FLAG_NOT_TOUCH_MODAL |
WindowManagerLayoutParams.FLAG_KEEP_SCREEN_ON,
WindowManagerLayoutParams.TRANSLUCENT // 半透明
);
params.gravity = Gravity.TOP | Gravity.LEFT; // 设置悬浮窗的位置
params.x = 100; // 悬浮窗的 x 坐标
params.y = 200; // 悬浮窗的 y 坐标
// 确保 windowManager 实例是正确的
if (windowManager) {
console.log(JSON.stringify(windowManager))
windowManager.addView(textView, params);
} else {
console.error("windowManager 实例未正确初始化。");
}
// 如果需要,可以在这里处理悬浮窗的交互逻辑
// 移除悬浮窗的示例代码(如果需要)
// windowManager.removeView(textView);
});
输出:
10:22:09.769{"UUID":"Invocation148323725","TYPE":"JSBObject","className":"android.view.WindowManagerImpl"}
10:22:09.769 Uncaught TypeError: windowManager.addView is not a function
1 个回复
6***@qq.com
最后怎么解决的