由于PC窗口存在标题栏,因此设置PC窗口透明背景色需要setWindowBackgroundColor与setWindowContainerColor搭配使用,setWindowContainerColor设置标题栏透明,setWindowBackgroundColor设置内容区域背景色透明,方可实现整个PC窗口透明效果。示例代码如下:
"requestPermissions": [
{
'name': "ohos.permission.SET_WINDOW_TRANSPARENT"
}
], import { window } from '@kit.ArkUI'; @Entry @Component struct Index { private message: string = 'Hello World'; uiContext = this.getUIContext(); // 在aboutToAppear()生命周期中设置PC窗口背景颜色 aboutToAppear(): void { window.getLastWindow(this.uiContext?.getHostContext(), (err, data) => { try { data.setWindowBackgroundColor('#00000000'); data.setWindowContainerColor('#00000000', '#FF000000'); } catch (exception) { console.error(`Failed to set the background color. Cause code: ${exception.code}, message: ${exception.message}`); } }); } build() { RelativeContainer() { Text(this.message) .id('Page2HelloWorld') .fontSize(50) .fontColor(Color.White) .fontWeight(FontWeight.Bold) .alignRules({ center: { anchor: '__container__', align: VerticalAlign.Center }, middle: { anchor: '__container__', align: HorizontalAlign.Center } }); }; } }