HarmonyOS鸿蒙Next中如何打开透明背景的enrty组件
HarmonyOS鸿蒙Next中如何打开透明背景的enrty组件 如何打开透明背景的enrty组件
您可以使用setwindowbackgroundcolor
router路由模式请参考下面demo:
// Page1.ets
import window from '@ohos.window';
@Entry
@Component
struct Page2 {
@State message: string = 'page Page2';
onPageHide() {
console.log("pageHide")
}
build() {
Row() {
Column() {
Text(this.message)
.fontSize(50)
.fontWeight(FontWeight.Bold)
Button("pageB").onClick(() => {
let windowStege: window.WindowStage = AppStorage.get("windowStage") as window.WindowStage;
windowStege.createSubWindow("hello", (err, win) => {
win.setUIContent('pages/Page2');
win.showWindow();
})
})
}
.width('100%')
}
.height('100%')
.backgroundColor(Color.Pink)
}
}
// Page2.ets
import window from '@ohos.window';
@Entry
@Component
struct Index {
@State message: string = 'page Index';
aboutToAppear() {
window.findWindow("hello").setWindowBackgroundColor("#00000000")
}
onBackPress() {
window.findWindow("hello").destroyWindow().then(res => {
console.log("destroyWindow success")
}).catch(() => {
console.log("destroyWindow fail")
})
return true
}
build() {
Row() {
Column() {
Text(this.message)
.fontSize(50)
.fontWeight(FontWeight.Bold)
}
.width('100%')
.backgroundColor("#00000000")
}
.alignItems(VerticalAlign.Top)
.height('100%')
.backgroundColor("#80ffffff")
}
}
注意需要在EntryAbility里面添加关键代码那一行。
onWindowStageCreate(windowStage: window.WindowStage)
: void {
// Main window is created, set main page for this ability
hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate');
windowStage.loadContent(
'pages/Page231206095213071',
(err, data) => {
if (err.code) {
hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? '');
return;
}
AppStorage.setAndLink("windowStage", windowStage); // 关键代码
hilog.info(0x0000, 'testTag', 'Succeeded in loading the content. Data: %{public}s', JSON.stringify(data) ?? '');
}
);
}
NavDestination支持Dialog类型页面:
NavDestinationMode.STANDARD: 标准类型
NavDestinationMode.DIALOG: 默认透明。
win.setUIContent无法传参,该接口需要在loadContent()或setUIContent()调用生效后使用。
这段代码必须要加上才能返回,传参通过LocalStorage传递状态属性给加载的页面
onBackPress() {
window.findWindow("WindowBackgroundColor").destroyWindow().then(res => {
console.log("destroyWindow success")
}).catch(() => {
console.log("destroyWindow fail")
})
return true
}
更多关于HarmonyOS鸿蒙Next中如何打开透明背景的enrty组件的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
在HarmonyOS鸿蒙Next中,打开透明背景的entry
组件可以通过设置AbilitySlice
的样式来实现。具体步骤如下:
-
创建
AbilitySlice
:首先,在MainAbility
中创建一个AbilitySlice
,并在onStart
方法中设置该AbilitySlice
为当前显示的页面。 -
设置透明背景:在
AbilitySlice
的布局文件中,设置根布局的背景为透明。可以使用ohos:background_element
属性,将其值设置为透明颜色或透明图片。 -
修改
config.json
:在config.json
文件中,找到对应的ability
配置项,将"supportWindowMode": "full_screen"
改为"supportWindowMode": "floating"
,以确保页面支持浮动窗口模式。 -
设置窗口属性:在
AbilitySlice
的onStart
方法中,通过Window
对象设置窗口的透明度和背景。可以使用Window.setTransparent(true)
和Window.setBackgroundColor(Color.TRANSPARENT)
来实现。 -
测试运行:编译并运行应用,查看
entry
组件是否显示为透明背景。
通过以上步骤,可以在HarmonyOS鸿蒙Next中实现透明背景的entry
组件。
在HarmonyOS鸿蒙Next中,打开透明背景的entry
组件可以通过设置Component
的background_element
属性来实现。具体步骤如下:
- 创建
entry
组件:在布局文件中定义entry
组件。 - 设置透明背景:在代码中通过
setBackgroundElement
方法将背景设置为透明。例如:entry.setBackgroundElement(new Element().setColor(Color.TRANSPARENT));
这样,entry
组件的背景就会变为透明。