HarmonyOS NEXT开发教程:全局悬浮窗

简介: 今天跟大家分享一下HarmonyOS开发中的悬浮窗。

今天跟大家分享一下HarmonyOS开发中的悬浮窗。
image.png

对于悬浮窗,可能有的同学会想到使用层叠布局是否可以实现,将悬浮窗叠在导航栏组件Tabs上,像这样:

Stack({alignContent:Alignment.BottomEnd}){
  Tabs({barPosition:BarPosition.End}){
    TabContent(){
      Text('page1')
        .fontColor(Color.Black)
        .fontSize(40)
    }
    .tabBar(this.Tabbar())
    TabContent(){
      Page2()
    }
    .tabBar(this.Tabbar())

  }
  Row(){
   //这是悬浮窗
  }
  .width(60)
  .height(60)
  .borderRadius(30)
  .backgroundColor(Color.Blue)
}

这样的布局在push到下一个页面时悬浮窗就会消失,所以是行不通的。
对于悬浮窗鸿蒙系统有专属的创建方法,就是使用createSubWindow创建子窗口,这个子窗口会悬浮在整个应用上方。具体实现代码如下:

windowStage.createSubWindow('floatWindow',(err: BusinessError, data) => {
})

在创建完成后,可以设置悬浮窗的尺寸、位置和内容等属性,要注意这里的单位是px,且只能传正整数类型:

//尺寸
data.resize(300,300,(err: BusinessError) => {
})
//位置
data.moveWindowTo(400,400,(err: BusinessError) => {
})
//展示
data.showWindow((err: BusinessError) => {
});

有的时候你可能需要设置悬浮窗是圆形,办法是先去对应的内容页面设置圆角后发现还是会有白色的背景,然后把窗口背景色设置为透明,这个方法最好在setUIContent的回调中设置:

data?.setWindowBackgroundColor('#00000000');

当需要关闭悬浮窗时,可以调用destroyWindow方法进行销毁:

window.findWindow(FloatUntil.windowName).destroyWindow()

以上就是悬浮窗的常见使用方法,为了便于使用,我把以上方法进行了简单的封装,可以一行代码就实现悬浮窗的展示、移动、关闭等操作:

import EntryAbility from "../entryability/EntryAbility";
import { BusinessError } from "@kit.BasicServicesKit";
import { display, window } from "@kit.ArkUI";
export class FloatUntil{
  static  screen_width = display.getDefaultDisplaySync().width
  static screen_height = display.getDefaultDisplaySync().height
  static float_size = 420
  static  float_positon_x = FloatUntil.screen_width - FloatUntil.float_size - 40
  static  float_positon_y = FloatUntil.screen_height - FloatUntil.float_size - 440
  static  windowName = 'floatWindow'
 static  creatAndShowSubWindow(){
    EntryAbility.gloabalWindowStage.createSubWindow(FloatUntil.windowName, (err: BusinessError, data) => {
      let errCode: number = err.code;
      if (errCode) {
        return;
      }
      //位置、尺寸单位是px,只支持正整数
      data.moveWindowTo(FloatUntil.float_positon_x, FloatUntil.float_positon_y, (err: BusinessError) => {
        let errCode: number = err.code;
        if (errCode) {
          return;
        }
        console.info('Succeeded in moving the window.');
      });
      data.resize(FloatUntil.float_size, FloatUntil.float_size, (err: BusinessError) => {
        let errCode: number = err.code;
        if (errCode) {
          return;
        }
      });
      //悬浮窗是否可触
      data.setWindowTouchable(true);
      data.setUIContent("pages/FloatWindow", (err: BusinessError) => {
        let errCode: number = err.code;
        if (errCode) {
          console.error('Failed to load the content. Cause:' + JSON.stringify(err));
          return;
        }
        console.info('Succeeded in loading the content.');
        //设置窗口背景色透明,只有在这调用才不会报错
        data?.setWindowBackgroundColor('#00000000');
        data.showWindow((err: BusinessError) => {
          let errCode: number = err.code;
          if (errCode) {
            console.error('Failed to show the window. Cause: ' + JSON.stringify(err));
            return;
          }
          console.info('Succeeded in showing the window.');
        });
      });
    })
  }
  static moveWindowTo(x:number,y:number){
    window.findWindow(FloatUntil.windowName).moveWindowTo(x,y)
  }
  static destroyFloatWindow(){
     window.findWindow(FloatUntil.windowName).destroyWindow()
  }
}

##HarmonyOS语言##ArkTS##工具效率

相关文章
|
13天前
|
人工智能 自然语言处理 算法
编程简单了,部署依旧很难|Karpathy 演讲的 5 点解读
本文总结了 Andrej Karpathy 在 YC AI Startup School 的分享核心观点,涵盖软件发展的三个阶段、LLM 的定位与挑战、Agent 的产品工程思路以及编程与部署的未来趋势。内容适合 AI 领域从业者参考,强调通过提升工程能力实现 AI 应用的稳定性与可控性。完整视频链接附于文末,便于深入学习。
154 15
|
8天前
|
存储 运维 安全
Docker化运维:容器部署的实践指南
Docker化运维:容器部署的实践指南
|
18天前
|
人工智能 开发者
2025魔搭开发者大会!来了!
2025魔搭开发者大会!来了!
682 1
|
人工智能 自然语言处理 IDE
童心码力—小游戏场景
仅需一句自然语言描述就能快速生成!借助通义灵码强大的 AI 代码理解与生成能力,即使是编程新手也能轻松创作属于自己的小游戏~
|
13天前
|
传感器 人工智能 IDE
通义灵码用户说 | 编程智能体+MCP加持,秒查附近蜜雪冰城
通义灵码现已全面支持Qwen3,新增智能体模式,具备自主决策、环境感知、工具使用等能力,可端到端完成编码任务。支持问答、文件编辑、智能体多模式自由切换,结合MCP工具与记忆功能,提升开发效率。AI IDE重构编程流程,让开发更智能高效。
207 20
|
19天前
|
存储 固态存储 Linux
VMware ESXi 9.0 正式版发布下载 - 领先的裸机 Hypervisor
VMware ESXi 9.0 正式版发布下载 - 领先的裸机 Hypervisor
290 1
VMware ESXi 9.0 正式版发布下载 - 领先的裸机 Hypervisor
|
27天前
|
人工智能 IDE 算法
通义灵码 AI IDE 上线!智能体+MCP 从手动调用工具过渡到“AI 主动调度资源”
编程智能体与 MCP 的结合,不只是“工具+助手”,而是一次范式上的跃迁——从“手动调用工具”过渡到“AI 主动调度资源”。
|
2月前
|
人工智能 弹性计算 自然语言处理
Bolt.diy:AI全栈开发,一句话生成网站
Bolt.diy是一个AI驱动的Web开发工具,支持通过自然语言Prompt直接生成、编辑和部署全栈应用,无需本地环境配置。本次实验基于DeepSeek于2025年5月28日发布了DeepSeek-R1-0528升级版本,显著提升了编程能力。
Bolt.diy:AI全栈开发,一句话生成网站
|
18天前
|
知识图谱
中文维基百科Wikipedia镜像网站
为解决中文维基百科在国内访问延迟高、加载不稳定的问题,张永豪与联合库UNHub基于Cloudflare Workers开发了内容加速方案。该方案优化了中文维基百科的全球分发效率,提升访问速度与稳定性,并部署至二级域名https://wiki.zyhorg.ac.cn/。同时,还创建了引导页面https://zyhorg.ac.cn/Wikipedia/,让用户更便捷地享受高效、稳定的知识浏览体验。
655 0