当前位置: 首页 > news >正文

鸿蒙:更改状态栏、导航栏颜色

前言:

最近在学习深色模式切换的时候,注意到状态栏颜色是可以自行设置的,在这里,我做下分享。

官方文档参考下方:

https://developer.huawei.com/consumer/cn/doc/architecture-guides/architecture-v1-3_2-ts_45-0000002378096446https://developer.huawei.com/consumer/cn/doc/architecture-guides/architecture-v1-3_2-ts_45-0000002378096446

【这里给大家提供两种方案】

方案一:

逻辑写在组件内部,运行效果和代码如下:

Index.ets

import window from '@ohos.window';
import { BusinessError } from '@kit.BasicServicesKit';@Entry
@Component
struct Index {changeColor(){let windowClass: window.Window | undefined = undefined;window.getLastWindow(getContext(), (err: BusinessError, data) => {windowClass = datalet SystemBarProperties: window.SystemBarProperties = {statusBarColor: '#2b2b2b',statusBarContentColor: '#fc5531'};try {let promise = windowClass.setWindowSystemBarProperties(SystemBarProperties);promise.then(() => {console.info('Succeeded in setting the system bar properties.');}).catch((err: BusinessError) => {console.error(`Failed to set the system bar properties. Cause code: ${err.code}, message: ${err.message}`);});} catch (exception) {console.error(`Failed to set the system bar properties. Cause code: ${exception.code}, message: ${exception.message}`);}})
}build() {Column(){Button("变更颜色").onClick(()=>{this.changeColor()})}.justifyContent(FlexAlign.Center).height('100%').width('100%')}
}

方案二:

逻辑抽离到一个工具类中,运行效果如下:

代码如下:

StatusBarManager.ets

import { common } from "@kit.AbilityKit";
import { window } from "@kit.ArkUI";
import { BusinessError } from "@kit.BasicServicesKit";export class StatusBarManager {static handleStatusBarAction(context: common.BaseContext, p: string): void {try {interface ParamsType {action: string;args: Array<string>;}const params: ParamsType = JSON.parse(p);const action: string = params.action;const args: Array<string> = params.args;let statusBarColor = '#007dff';if (action === 'backgroundColorByHexString') {statusBarColor = args[0] || statusBarColor;}const systemBarProperties: window.SystemBarProperties = {statusBarColor: statusBarColor,statusBarContentColor: '#F7CE00',};StatusBarManager.setStatusBarBgColor(context, systemBarProperties);} catch (error) {console.error('Failed to parse or handle status bar action. Error:');}}private static setStatusBarBgColor(context: common.BaseContext,systemBarProperties: window.SystemBarProperties): void {window.getLastWindow(context).then((windowClass: window.Window) => {try {windowClass.setWindowSystemBarProperties(systemBarProperties);console.info('Status bar color updated successfully.');} catch (exception) {console.error('Failed to set the system bar properties. Cause: ');}}).catch((error: BusinessError) => {console.error('Failed to get last window. Error: ');});}
}

Index.ets

import window from '@ohos.window';
import { BusinessError } from '@kit.BasicServicesKit';
import { StatusBarManager } from './StatusBarManager';
import { common } from '@kit.AbilityKit';@Entry
@Component
struct Index {changeColor(){let windowClass: window.Window | undefined = undefined;window.getLastWindow(getContext(), (err: BusinessError, data) => {windowClass = datalet SystemBarProperties: window.SystemBarProperties = {statusBarColor: '#2b2b2b',statusBarContentColor: '#fc5531'};try {let promise = windowClass.setWindowSystemBarProperties(SystemBarProperties);promise.then(() => {console.info('Succeeded in setting the system bar properties.');}).catch((err: BusinessError) => {console.error(`Failed to set the system bar properties. Cause code: ${err.code}, message: ${err.message}`);});} catch (exception) {console.error(`Failed to set the system bar properties. Cause code: ${exception.code}, message: ${exception.message}`);}})
}build() {Column(){Button("变更颜色").onClick(()=>{StatusBarManager.handleStatusBarAction(this.getUIContext().getHostContext() as common.UIAbilityContext, '{}')})}.justifyContent(FlexAlign.Center).height('100%').width('100%')}
}


文章转载自:

http://AEg9REso.xrqkm.cn
http://cPYRBRDb.xrqkm.cn
http://XZmSYgpT.xrqkm.cn
http://tz1xd4as.xrqkm.cn
http://XMiMQ4s6.xrqkm.cn
http://kH8u66bd.xrqkm.cn
http://OTecjwSz.xrqkm.cn
http://S5HPCUVa.xrqkm.cn
http://J9dVpUYy.xrqkm.cn
http://0bBrzqF4.xrqkm.cn
http://llPwOrK7.xrqkm.cn
http://Y5hLXlxc.xrqkm.cn
http://75aIx4DI.xrqkm.cn
http://W8EoXAU9.xrqkm.cn
http://Ei6v2j4e.xrqkm.cn
http://WsPp0kf6.xrqkm.cn
http://ujbpPcvw.xrqkm.cn
http://hKSlSnve.xrqkm.cn
http://kLi1zic4.xrqkm.cn
http://2wd1GNaG.xrqkm.cn
http://HSY5brr9.xrqkm.cn
http://LhjdZlXW.xrqkm.cn
http://7bJfI3KL.xrqkm.cn
http://QFEvtDuf.xrqkm.cn
http://xB1kVKYR.xrqkm.cn
http://Xum0Jz34.xrqkm.cn
http://bp4huf7F.xrqkm.cn
http://OBm2MMU7.xrqkm.cn
http://1IexuPY2.xrqkm.cn
http://lV2rioDP.xrqkm.cn
http://www.dtcms.com/a/373624.html

相关文章:

  • [数据结构——lesson4.双向链表]
  • 集成学习:从理论到实践的全面解析
  • 机器学习-集成学习
  • 集成学习简介
  • JDK 17、OpenJDK 17、Oracle JDK 17 的说明
  • VM中CentOS 7密码重置
  • 科技信息差(9.8)
  • MATLAB的数值计算(三)曲线拟合与插值
  • 城市脉搏中的“绿色卫士”:当智能科技邂逅城市清洁
  • linux播放视频出现需要MPEG-4 AAC解码器,H.265(Main Profile)解码器,但是没有安装
  • ARM工作模式、汇编学习
  • 【入门级-算法-6、排序算法:选择排序】
  • React state在setInterval里未获取最新值的问题
  • Linux 物理机如何区分 SSD 与 HDD ——以 DELL PERC H730 Mini 为例
  • AP和stage模式差异
  • 支持生成一维条形码Extend .NET
  • 企业级固态硬盘——U.2接口技术
  • 【Android虚拟摄像头】七、安卓15系统实现虚拟摄像头
  • FxSound:提升音频体验,让音乐更动听
  • Don‘t Sleep:保持电脑唤醒,确保任务不间断
  • android/java中,配置更改导致activity销毁重建的解决方法
  • C++day8作业
  • 【CI/CD】GitHub Actions 快速入门
  • 如何在安卓手机/平板上找到下载文件?
  • Claude Code Windows 原生版安装指南
  • AR技术:多行业数字化转型的加速引擎
  • C++初阶(4)类和对象(上)
  • SpringAI企业级应用开发面试全流程解析:核心技术、架构落地与业务场景实战
  • 从旋转位置编码RoPE到YaRN的原理与实现
  • xfs inode cluster lock order导致的死锁