对于原生开发的应用,深色模式适配是开发过程中常见的业务场景,系统可以通过状态栏中的深色模式开关配置系统的颜色模式,当系统颜色模式方式变化时,应用经常会遇到如下的业务诉求:
效果
普通暗夜模式
方案
1.基于资源文件的组件颜色适配:
自定义两套颜色资源(resources/dark/element/color.json和resources/base/element/color.json),通过$r的方式加载颜色资源的key值。通过系统资源实现,开发者可直接使用的系统预置资源,即分层参数,同一资源ID在设备类型、深浅色等不同配置下有不同的取值。通过使用系统资源,不同的开发者可以开发出具有相同视觉风格的应用,不需要自定义2份颜色资源,在深浅色模式下也会自动切换成不同的颜色值。
2.基于媒体文件的图片资源适配:
3.页面状态栏的适配:
此处的代码要用到窗口的属性来设置状态栏图标的颜色,写在entry下(参考此章核心代码1);值得一提的是,barContentColor并不支持使用$r的方式加载颜色资源的key值,它是一个string类型的,因此,这里的是不是暗夜模式就要开发者自己去写代码判断。
4.基于Web组件适配:
支持对前端页面进行深色模式设置,通过darkMode 接口可以配置跟随系统。若网页未定义深色样式,则需开启强制深色模式 forceDarkAccess 使用。需要注意的要让网页跟随系统,请设置WebDarkMode为Auto。
核心代码:
1. 基于资源文件的组件颜色适配:
{
"name": "list_background",
"value": "#FFFFFF"
},
2. resources/dark/element/color.json
{
"name": "list_background",
"value": "#212224"
},
3. 通过$r的方式加载颜色资源的key值。
.backgroundColor($r('app.color.list_background'))
**2.基于媒体文件的图片资源适配:
1.普通png图片
路径resources/base/media/loading.png
路径resources/dark/media/loading.png
Image($r("app.media.loading"))
2.svg图片
resources/base/color
{
"name": "ic_public_back_color",
"value": "#212121"
}
esources/dark/color
{
"name": "ic_public_back_color",
"value": "#dcdcdc"
}
Image($r('app.media.ic_public_back'))
.fillColor($r('app.color.ic_public_back_color'))
3.页面状态栏的适配:
@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
};
})
}
4.基于Web组件适配:(注意开启了强制网页适配,原因是指定网页没有定义深色样式)
@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
};
})
}
效果
方案
案例代码
@Entry
@Component
struct TogglePage2 {
@State isDarkMode: boolean = false
build() {
Column() {
Toggle({ type: ToggleType.Switch ,isOn:this.isDarkMode})//isOn 属性值在有触发刷新页面的场景中,不要省略
.onChange((isOn: boolean) => {
console.log('Toggle.onChange2: isOn', isOn)
this.isDarkMode = isOn
getContext(this).getApplicationContext().setColorMode(this.isDarkMode?0:1) //触发二次渲染,渲染不给isOn 熟悉赋值会给默认值false,导致状态不对
})
}.width("100%").height("100%").padding(32)
}
}
2.Web组件通过darkMode和forceDarkAccess属性,配置是否强制接入深色模式案例代码,设置darkMode为On即可
import web_webview from '@ohos.web.webview'
@Entry
@Component
struct WebComponent {
controller: web_webview.WebviewController = new web_webview.WebviewController()
@State mode: WebDarkMode = WebDarkMode.On
@State access: boolean = true
build() {
Column() {
Web({ src: 'www.xxx.com', controller: this.controller }).darkMode(this.mode).forceDarkAccess(this.access)
}
}
}
如果你觉得这篇内容对你还蛮有帮助,我想邀请你帮我三个小忙:
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。