可监听组件onVisibleAreaChange事件,通过setWindowSystemBarProperties主动设置窗口状态栏属性,达到状态栏显示颜色反转的效果。
import { ConfigurationConstant, UIAbility } from '@kit.AbilityKit'; import { hilog } from '@kit.PerformanceAnalysisKit'; import { window } from '@kit.ArkUI'; const DOMAIN = 0x0000; export default class EntryAbility extends UIAbility { onCreate(): void { try { this.context.getApplicationContext().setColorMode(ConfigurationConstant.ColorMode.COLOR_MODE_NOT_SET); } catch (err) { hilog.error(DOMAIN, 'testTag', 'Failed to set colorMode. Cause: %{public}s', JSON.stringify(err)); } hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onCreate'); } onDestroy(): void { hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onDestroy'); } onWindowStageCreate(windowStage: window.WindowStage): void { // Main window is created, set main page for this ability hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); try { let windowClass = windowStage.getMainWindowSync(); let isLayoutFullScreen = true; windowClass.setWindowLayoutFullScreen(isLayoutFullScreen); AppStorage.setOrCreate('windowClass', windowClass); } catch (exception) { console.error(`Failed to obtain the main window. Cause code: ${exception.code}, message: ${exception.message}`); } windowStage.loadContent('pages/Index', (err) => { if (err.code) { hilog.error(DOMAIN, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err)); return; } hilog.info(DOMAIN, 'testTag', 'Succeeded in loading the content.'); }); } onWindowStageDestroy(): void { // Main window is destroyed, release UI related resources hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); } onForeground(): void { // Ability has brought to foreground hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onForeground'); } onBackground(): void { // Ability has back to background hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onBackground'); } };
import { window } from '@kit.ArkUI'; @Entry @Component struct CommonTopBar { private windowClass = AppStorage.get<window.Window>('windowClass') as window.Window; build() { Scroll() { Column() { Text('浅色页面部分') .fontSize(50) .height(500) .fontWeight(FontWeight.Bold) .onVisibleAreaChange([0.0, 0.1], (isExpanding: boolean) => { if (isExpanding) { this.windowClass.setWindowSystemBarProperties({ statusBarContentColor: '#ff000000' }); } if (!isExpanding) { this.windowClass.setWindowSystemBarProperties({ statusBarContentColor: '#fffafafa' }); } }); Column() { Text('深色页面部分') .fontSize(50) .fontColor(Color.White) .height(500); } .height(1500) .width('100%') .backgroundColor(Color.Black); }; } .height('100%') .width('100%'); } }