W3Cschool
恭喜您成為首批注冊(cè)用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
微信從iOS客戶端 7.0.12、Android客戶端 7.0.13 開始正式支持 DarkMode,小程序也從基礎(chǔ)庫 v2.11.0、開發(fā)者工具 1.03.2004271 開始,為開發(fā)者提供小程序內(nèi)的 DarkMode 適配能力。
在app.json中配置darkmode為true,即表示當(dāng)前小程序已適配 DarkMode,所有基礎(chǔ)組件均會(huì)根據(jù)系統(tǒng)主題展示不同的默認(rèn)樣式,navigation bar 和 tab bar 也會(huì)根據(jù)下面的配置自動(dòng)切換。
當(dāng)app.json中配置darkmode為true時(shí),小程序部分配置項(xiàng)可通過變量的形式配置,在變量配置文件中定義不同主題下的顏色或圖標(biāo),方法如下:
支持通過變量配置的屬性:
theme.json用于顏色主題相關(guān)的變量定義,需要先在themeLocation中配置theme.json的路徑,否則無法讀取變量配置。
配置文件須包含以下屬性:
屬性 | 類型 | 必填 | 描述 |
---|---|---|---|
light | object | 是 | 淺色模式下的變量定義 |
dark | object | 是 | 深色模式下的變量定義 |
light和dark下均可以key: value的方式定義變量名和值,例如:
{
"light": {
"navBgColor": "#f6f6f6",
"navTxtStyle": "black"
},
"dark": {
"navBgColor": "#191919",
"navTxtStyle": "white"
}
}
完成定義后,可在全局配置或頁面配置的相關(guān)屬性中以@開頭引用,例如:
// 全局配置
{
"window": {
"navigationBarBackgroundColor": "@navBgColor",
"navigationBarTextStyle": "@navTxtStyle"
}
}
// 頁面配置
{
"navigationBarBackgroundColor": "@navBgColor",
"navigationBarTextStyle": "@navTxtStyle"
}
配置完成后,小程序框架會(huì)自動(dòng)根據(jù)系統(tǒng)主題,為小程序展示對(duì)應(yīng)主題下的顏色。
app.json(示例省略了主題相關(guān)以外的配置項(xiàng))
{
"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的返回結(jié)果中會(huì)包含theme屬性,值為light或dark。
如果app.json未聲明"darkmode": true,則無法獲取到theme屬性(即theme為undefined)。
支持2種方式:
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 */
}
微信開發(fā)者工具 1.03.2004271 版本開始已支持 DarkMode 調(diào)試,在模擬器頂部可以切換 深色/淺色 模式進(jìn)行,如圖:
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: