小白教程

 找回密码
 立即注册

微信小程序 DarkMode适配指南

发布者: 小白教程



配置示例

app.json(示例省略了主题相关以外的配置项)

{
    "window": {
        "navigationBarBackgroundColor": "@navBgColor",
        "navigationBarTextStyle": "@navTxtStyle",
        "backgroundColor": "@bgColor",
        "backgroundTextStyle": "@bgTxtStyle",
        "backgroundColorTop": "@bgColorTop",
        "backgroundColorBottom": "@bgColorBottom"
    },
    "tabBar": {
        "color": "@tabFontColor",
        "selectedColor": "@tabSelectedColor",
        "backgroundColor": "@tabBgColor",
        "borderStyle": "@tabBorderStyle",
        "list": [{
            "iconPath": "@iconPath1",
            "selectedIconPath": "@selectedIconPath1"
        }, {
            "iconPath": "@iconPath2",
            "selectedIconPath": "@selectedIconPath2"
        }]
    }
}

theme.json

{
    "light": {
        "navBgColor": "#f6f6f6",
        "navTxtStyle": "black",
        "bgColor": "#ffffff",
        "bgTxtStyle": "light",
        "bgColorTop": "#eeeeee",
        "bgColorBottom": "#efefef",
        "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",
        "bgColor": "#1f1f1f",
        "bgTxtStyle": "dark",
        "bgColorTop": "#191919",
        "bgColorBottom": "#1f1f1f",
        "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",
    }
}

获取当前系统主题

如果app.json中声明了"darkmode": true,wx.getSystemInfo或wx.getSystemInfoSync的返回结果中会包含theme属性,值为light或dark。

如果app.json未声明"darkmode": true,则无法获取到theme属性(即theme为undefined)。

监听主题切换事件

支持2种方式:

  1. 在App()中传入onThemeChange回调方法,主题切换时会触发此回调
  2. 通过wx.onThemeChange监听主题变化,wx.offThemeChange取消监听

WXSS 适配

WXSS中,支持通过媒体查询 prefers-color-scheme 适配不同主题,与 Web 中适配方式一致,例如:


/* 一般情况下的样式 begin */
.some-background {
    background: white;
}
.some-text {
    color: black;
}
/* 一般情况下的样式 end */

@media (prefers-color-scheme: dark) {
    /* DarkMode 下的样式 start */
    .some-background {
        background: #1b1b1b;
    }
    .some-text {
        color: #ffffff;
    }
    /* DarkMode 下的样式 end */
}

开发者工具调试

微信开发者工具 1.03.2004271 版本开始已支持 DarkMode 调试,在模拟器顶部可以切换 深色/浅色 模式进行,如图:

darkmode_devtool

Bug & Tip

  1. tip: 需要注意的是,WXSS 中的媒体查询不受app.json中的darkmode开关配置影响,只要微信客户端(iOS 7.0.12、Android 7.0.13)支持 DarkMode,无论是否配置"darkmode": true,在系统切换到 DarkMode 时,媒体查询都将生效。
  2. tip: 主题切换事件需要在配置"darkmode": true时,才会触发。
  3. bug: iOS 7.0.12 在 light 模式中配置 tabBar 的borderStyle为white时可能会出现黑色背景的 bug,后续版本将会修复。
12
微信小程序教程
  1. 微信小程序 小程序简介

  2. 微信小程序 开始

  3. 小程序代码构成

  4. 小程序宿主环境

  5. 小程序协同工作和发布

  6. 配置小程序

  7. sitemap配置

  8. 微信小程序 场景值

  9. 微信小程序 注册小程序

  10. 微信小程序 注册页面

  11. 微信小程序 页面生命周期

  12. 微信小程序 页面路由

  13. 微信小程序 模块化

  14. 微信小程序 API

  15. 微信小程序 WXML

  16. 微信小程序 WXSS

  17. 微信小程序 WXS

  18. 微信小程序 事件系统

  19. 微信小程序 简单双向绑定

  20. 微信小程序 基础组件

  21. 微信小程序 获取界面上的节点信息

  22. 微信小程序 响应显示区域变化

  23. 微信小程序 动画

  24. 微信小程序 初始渲染缓存

  25. 微信小程序 运行环境

  26. 微信小程序 JavaScript支持情况

  27. 微信小程序 运行机制

  28. 微信小程序 更新机制

  29. 微信小程序 组件模板和样式

  30. 微信小程序 Component构造器

  31. 微信小程序 组件间通信与时间

  32. 微信小程序 组件生命周期

  33. 微信小程序 behaviors

  34. 微信小程序 组件间关系

  35. 微信小程序 数据监听器

  36. 微信小程序 纯数据字段

  37. 微信小程序 抽象节点

  38. 微信小程序 自定义组件扩展

  39. 微信小程序 开发第三方自定义组件

  40. 微信小程序 单元测试

  41. 微信小程序 开发插件

  42. 微信小程序 使用插件

  43. 微信小程序 插件调用API的限制

  44. 微信小程序 插件功能页

  45. 微信小程序 网络

  46. 微信小程序 文件系统

  47. 微信小程序 画布

  48. 微信小程序 分包加载

  49. 微信小程序 多线程Worker

  50. 微信小程序 服务端能力

  51. 微信小程序 自定义tabBar

  52. 微信小程序 周期性更新

  53. 微信小程序 数据预拉取

  54. 微信小程序 DarkMode适配指南

  55. 微信小程序 大屏适配指南

Archiver|手机版|小黑屋|小白教程 ( 粤ICP备20019910号 )

GMT+8, 2024-9-20 06:19 , Processed in 0.020571 second(s), 18 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc. Template By 【未来科技】【 www.wekei.cn 】

返回顶部