简体中文
设置应用主题
uni.setAppTheme,并不会帮助开发者自动实现整个应用的亮/暗主题切换,它的作用是:
当然组件作者也可以不监听onAppThemeChange,而是暴露主题切换API给开发者,由开发者监听主题切换,再调用组件的主题切换API。
目前uni-app x的内置组件和UI相关的API(比如showModal),并不会响应setAppTheme。组件是暴露了样式属性供开发者自行设置,Modal相关API目前没有样式设置,后续会升级支持。
Web | Android | iOS |
---|---|---|
x | 4.18 | 4.18 |
名称 | 类型 | 必填 | 默认值 | 兼容性 | 描述 | ||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
options | SetAppThemeOptions | 是 | - | - | |||||||||||||||||||||||||||||||
|
名称 | 类型 | 必备 | 默认值 | 兼容性 | 描述 |
---|---|---|---|---|---|
theme | string | 是 | - | - | - |
名称 | 类型 | 必备 | 默认值 | 兼容性 | 描述 |
---|---|---|---|---|---|
errCode | 702001 | 2002000 | 是 | - | 错误码 - 702001 参数错误 - 2002000 未知错误 | |
errSubject | string | 是 | - | - | 统一错误主题(模块)名称 |
data | any | null | 否 | - | - | 错误信息中包含的数据 |
cause | Error | null | 否 | - | - | 源错误信息,可以包含多个错误,详见SourceError |
errMsg | string | 是 | - | - | - |
uni.setAppTheme({
theme: "auto",
success: function() {
console.log("设置appTheme为 auto 成功")
},
fail: function(e: IAppThemeFail) {
console.log("设置appTheme为 auto 失败,原因:", e.errMsg)
}
})
开启监听应用主题变化
版本历史调整
Web | Android | iOS |
---|---|---|
x | 4.18 | 4.18 |
名称 | 类型 | 必填 | 默认值 | 兼容性 | 描述 |
---|---|---|---|---|---|
callback | (res: AppThemeChangeResult) => void | 是 | - | - |
名称 | 类型 | 必备 | 默认值 | 兼容性 | 描述 |
---|---|---|---|---|---|
appTheme | string | 是 | - | 应用主题 |
类型 |
---|
number |
//callbackId 用于注销监听
val callbackId = uni.onAppThemeChange((res: AppThemeChangeResult) => {
console.log("onAppThemeChange", res.appTheme)
})
取消监听应用主题变化
Web | Android | iOS |
---|---|---|
x | 4.18 | 4.18 |
名称 | 类型 | 必填 | 默认值 | 兼容性 | 描述 |
---|---|---|---|---|---|
id | number | 是 | - | - |
val callbackId = uni.onAppThemeChange((res: AppThemeChangeResult) => {
console.log("onAppThemeChange", res.appTheme)
})
//...
//...
//注销监听
uni.offAppThemeChange(this.appThemeChangeId)
开启监听系统主题变化
Web | Android | iOS |
---|---|---|
x | 4.18 | 4.18 |
名称 | 类型 | 必填 | 默认值 | 兼容性 | 描述 |
---|---|---|---|---|---|
callback | (res: OsThemeChangeResult) => void | 是 | - | - |
名称 | 类型 | 必备 | 默认值 | 兼容性 | 描述 |
---|---|---|---|---|---|
osTheme | string | 是 | - | 系统主题 |
类型 |
---|
number |
//callbackId 用于注销监听
val callbackId = uni.onOsThemeChange((res: OsThemeChangeResult)=> {
console.log("onOsThemeChange---", res.osTheme)
})
注意:
dark
,更低版本无法获取、监听OS的主题。取消监听系统主题变化
Web | Android | iOS |
---|---|---|
x | 4.18 | 4.18 |
名称 | 类型 | 必填 | 默认值 | 兼容性 | 描述 |
---|---|---|---|---|---|
id | number | 是 | - | - |
val callbackId = uni.onOsThemeChange((res: OsThemeChangeResult)=> {
console.log("onOsThemeChange---", res.osTheme)
})
...
...
//注销监听
uni.offOsThemeChange(callbackId)
该 API 不支持 Web,请运行 hello uni-app x 到 App 平台体验
<template>
<view class="uni-padding-wrap">
<view class="uni-common-mt item-box">
<text>osTheme:</text>
<text id="theme">{{ osTheme }}</text>
</view>
<view class="uni-common-mt item-box">
<text>应用当前主题:</text>
<text id="theme">{{ appTheme }}</text>
</view>
<view>
<view class="uni-title uni-common-mt">
<text class="uni-title-text"> 修改appTheme主题(此处仅为演示API,本应用并未完整适配暗黑模式) </text>
</view>
</view>
<view class="uni-list uni-common-pl">
<radio-group @change="radioChange" class="radio-group">
<radio class="uni-list-cell uni-list-cell-pd radio" v-for="(item, index) in items" :key="item"
:class="index < items.length - 1 ? 'uni-list-cell-line' : ''" :value="item"
:checked="index === current">
{{ item }}
</radio>
</radio-group>
</view>
</view>
</template>
<script>
export default {
data() {
return {
osThemeChangeId: 0,
appThemeChangeId: 0,
osTheme: "light" as string,
appTheme: "light" as string,
current: 0,
items: [
"light",
"dark",
"auto"
] as string[]
}
},
methods: {
bindOsThemeChange(): number {
//注册osTheme变化监听
return uni.onOsThemeChange((res: OsThemeChangeResult)=> {
this.osTheme = res.osTheme
})
},
bindAppThemeChange(): number {
//注册appTheme变化监听
return uni.onAppThemeChange((res: AppThemeChangeResult) => {
this.appTheme = res.appTheme
})
},
radioChange(e : UniRadioGroupChangeEvent) {
const theme = e.detail.value
this.setAppTheme(theme)
uni.showToast({
icon: 'none',
title: '当前选中:'+theme,
})
},
setAppTheme(value: string) {
uni.setAppTheme({
theme: value,
success: function() {
console.log("设置appTheme为", value, "成功")
},
fail: function(e: IAppThemeFail) {
console.log("设置appTheme为", value, "失败,原因:", e.errMsg)
}
})
}
},
onReady() {
uni.getSystemInfo({
success: (res:GetSystemInfoResult) => {
this.osTheme = res.osTheme!
this.appTheme = res.appTheme == "auto" ? res.osTheme! : res.appTheme!
this.current = this.items.indexOf(res.appTheme!)
}
})
this.osThemeChangeId = this.bindOsThemeChange()
this.appThemeChangeId = this.bindAppThemeChange()
},
onUnload() {
//注销监听
uni.offAppThemeChange(this.appThemeChangeId)
uni.offOsThemeChange(this.osThemeChangeId)
uni.showToast({
"position":"bottom",
"title":"已停止监听主题切换"
})
}
}
</script>
<style>
.item-box {
display: flex;
flex-direction: row;
justify-content: space-between;
}
.uni-list-cell {
justify-content: flex-start;
}
</style>
名称 | 类型 | 必备 | 默认值 | 兼容性 | 描述 |
---|---|---|---|---|---|
errMsg | string | 是 | - | - | 错误信息 |