微信小程序 DarkMode适配指南
作者: --时间: 2025-06-10 13:39:30
阅读量:246
若您想为您的小程序添加 DarkMode 适配功能,可以参考以下指南:
1. 开启 DarkMode
在 app.json 中将 darkmode 设置为 true,表示小程序已经适配了 DarkMode。此时小程序中的组件,包括导航栏和选项卡都会自动切换到与系统主题相匹配的默认样式。
2. 相关配置
当设置了 app.json 中的 darkmode 属性为 true 后,我们可以通过定义变量来设置特定颜色或图标。具体方法如下:
- 在 app.json 中配置 themeLocation,指定变量配置文件 theme.json 的路径。
- 在 theme.json 中定义相关变量。
- 在 app.json 或页面配置中可以通过 @ 号引用变量。
支持通过变量配置的属性包括:
- 全局配置的 window 属性与页面配置下的属性 navigationBarBackgroundColor、navigationBarTextStyle、backgroundColor、backgroundTextStyle、backgroundColorTop、backgroundColorBottom。
- 全局配置 Window.tabBar 属性中的 color 、 selectedColor、backgroundColor、borderStyle、list、iconPath、selectedIconPath。
变量配置文件示例:theme.json
请根据需求在此配置颜色主题相关的变量:
属性 | 类型 | 必填 | 描述 |
---|---|---|---|
light | object | 是 | 浅色模式下的变量定义 |
dark | object | 是 | 深色模式下的变量定义 |
在定义好变量后,可以在全局配置或页面配置的相关属性中以 @ 开头引用这些变量。
配置示例:
1. 在 app.json 中设置如下:
"window": {
"navigationBarBackgroundColor": "@navBgColor",
"navigationBarTextStyle": "@navTxtStyle"
},
"tabBar": {
"color": "@tabFontColor",
"selectedColor": "@tabSelectedColor",
"backgroundColor": "@tabBgColor",
"borderStyle": "@tabBorderStyle",
"list": [{
"iconPath": "@iconPath1",
"selectedIconPath": "@selectedIconPath1"
},
{
"iconPath": "@iconPath2",
"selectedIconPath": "@selectedIconPath2"
}]
}
2. 定义变量
{ "light": { "navBgColor": "#f6f6f6", "navTxtStyle": "black", "tabFontColor": "#000000", "tabSelectedColor": "#3cc51f", "tabBgColor": "#ffffff", "tabBorderStyle": "black", "iconPath1": "image/icon1_light.png", "selectedIconPath1": "image/selected_icon1_light.png", "iconPath2": "image/icon2_light.png", "selectedIconPath2": "image/selected_icon2_light.png" }, "dark": { "navBgColor": "#191919", "navTxtStyle": "white", "tabFontColor": "#ffffff", "tabSelectedColor": "#51a937", "tabBgColor": "#191919", "tabBorderStyle": "white", "iconPath1": "image/icon1_dark.png", "selectedIconPath1": "image/selected_icon1_dark.png", "iconPath2": "image/icon2_dark.png", "selectedIconPath2": "image/selected_icon2_dark.png" } }
上一篇:微信小程序 数据预拉取 下一篇:微信小程序 大屏适配指南