HarmonyOS NEXT开发教程:全局悬浮窗
【摘要】 今天跟大家分享一下HarmonyOS开发中的悬浮窗。对于悬浮窗,可能有的同学会想到使用层叠布局是否可以实现,将悬浮窗叠在导航栏组件Tabs上,像这样:Stack({alignContent:Alignment.BottomEnd}){ Tabs({barPosition:BarPosition.End}){ TabContent(){ Text('page1') ...
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)
}
//尺寸data.resize(300,300,(err: BusinessError) => {})
//位置data.moveWindowTo(400,400,(err: BusinessError) => {})
//展示data.showWindow((err: BusinessError) => {});
data?.setWindowBackgroundColor('#00000000');
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##工具效率#
【声明】本内容来自华为云开发者社区博主,不代表华为云及华为云开发者社区的观点和立场。转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息,否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)