在HarmonyOS NEXT开发中深色模式后状态栏看不到状态信息(时间、电量等)?
在HarmonyOS NEXT开发中遇到深色模式下状态栏信息不可见的问题,可通过以下方案解决:
状态栏颜色适配:
// 设置状态栏颜色和文字颜色
window.setStatusBarColor("#000000", (err, data) => {
if (!err) {
window.setStatusBarContentColor("#FFFFFF"); // 强制设置为白色
}
});
主题配置文件:
在resources/base/theme/theme.json
中添加:
"dark": {
"color": {
"status_bar_background": "#1A1A1A",
"status_bar_content": "#FFFFFF"
}
}
XML布局声明:
<ohos:config
xmlns:ohos="http://schemas.huawei.com/hap"
ohos:mode="dark">
<ohos:statusbar
ohos:background="#1A1A1A"
ohos:textColor="#FFFFFF"/>
</ohos:config>
关键排查点:
沉浸式状态栏
模式setStatusBarContentColor
方法使用系统提供的语义化颜色常量:
import color from '@ohos.color';
window.setStatusBarContentColor(color.Color.White);
若仍存在问题,建议使用hdc shell dumpsys window policy
命令查看当前状态栏的实际渲染参数。
页面状态栏的适配:
@State isDarkMode: boolean = false
@State barContentColor: string = ''
private context = getContext(this) as common.UIAbilityContext
// 状态栏适配黑夜模式
onPageShow(): void {
window.getLastWindow(getContext(this), (err, win) => {
//判断是否是暗夜模式(因为有三种)
if(this.context.config.colorMode==1){
this.isDarkMode = false
}
if(this.context.config.colorMode==0){
this.isDarkMode = true
}
//因为barContentColor只能取string类型的值,所以不能直接用resource资源来适配
this.barContentColor = this.isDarkMode ? '#ffffff' : '#000000'
let SystemBarProperties: window.SystemBarProperties = {
statusBarContentColor: this.barContentColor
};
})
}
具体案例可以参考链接:https://developer.huawei.com/consumer/cn/forum/topic/02081531...
1 回答879 阅读✓ 已解决
1 回答1.2k 阅读
1 回答1k 阅读
1 回答968 阅读
1 回答937 阅读
1 回答842 阅读
1 回答799 阅读
具体解决方案如下: