HarmonyOS 悬浮球功能?

有一个app全屏悬浮球的功能。

目前的做法是创建一个全局的subwindow,并将setWindowTouchable设置为false,但是该方法会将子组件的事件阻断,请问该怎么做?

伪代码:

//EntryAbility.ets
onWindowStageCreate(windowStage: window.WindowStage): void {
  windowStage.createSubWindow("mySubWindow", (err, windowClass) => {
  if (err.code > 0) {
  console.error("failed to create subWindow Cause:" + err.message)
  return;
}
try {
  // 设置子窗口加载页
  windowClass.setUIContent("pages/MySubWindow", () => {
    windowClass.setWindowBackgroundColor("#00000000")
  });
  windowClass.setWindowTouchable(false)
  // 设置子窗口左上角坐标
  windowClass.moveWindowTo(0, 0)
  // 设置子窗口大小

  // 展示子窗口
  windowClass.showWindow();
  // 设置子窗口全屏化布局不避让安全区
  windowClass.setWindowLayoutFullScreen(true);
} catch (err) {
  console.error("failed to create subWindow Cause:" + err)
}
})
}

//MySubWindow.ets

import { Event_Floating } from '../common/constants/EventConstants'
import { subscribeEvent2 } from '../common/mannagers/EventManger'
import { openH5 } from '../common/utils/H5Util'

@Entry
@Component
struct MySubWindow {
  // 悬浮按钮
  @State private backUrlOpacity: number = 1
  backUrl = ""

  build() {
    Column() {
      Column() {
        Image($r('app.media.back_url_icon'))
          .width(90)
          .height(47)
      }
      .offset({ y: -160, x: -16 })
      .opacity(this.backUrlOpacity)
      .onClick(() => { // 无响应
        openH5(this.backUrl)
        this.backUrlOpacity = 0
      })
      .animation({
        duration: 300
      })
    }
    .alignItems(HorizontalAlign.End)
    .justifyContent(FlexAlign.End)
    .height('100%')
    .width('100%')
    .backgroundColor(Color.Orange)
  }
}
阅读 537
1 个回答

您可以参考如下demo,可以参考如下实现悬浮窗功能

//EntryAbility.ets
onWindowStageCreate(windowStage: window.WindowStage): void {
  AppStorage.setOrCreate('windowStage',windowStage)
  // Main window is created, set main page for this ability
  hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate');
  windowStage.loadContent('pages/FloatWindow', (err) => {
  if (err.code) {
    hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? '');
    return;
  }
  hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.');
  });
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进