概述
针对应用,构建ArkUI应用级和页面级主题设置能力,并提供局部深浅色模式设置、动态换肤等功能。
本文提供如下场景:
- 自定义品牌色
- 应用级自定义品牌色
- 局部页面自定义主题风格
- 局部深浅色
自定义品牌色
CustomTheme 用于自定义主题,属性可选,只需要复写修改的部分,未修改内容继承于系统,参考 系统缺省token色值 。请参考:
import { CustomColors, CustomTheme } from '@kit.ArkUI'
export class AppColors implements CustomColors {
//自定义品牌色
brand: ResourceColor = '#FF75D9';
}
export class AppTheme implements CustomTheme {
public colors: AppColors = new AppColors()
}
export let gAppTheme: CustomTheme = new AppTheme()
设置应用级自定义品牌色
- 可在页面入口处统一设置,需要在页面build前执行 ThemeControl 。
其中, onWillApplyTheme 回调函数用于自定义组件获取当前生效的Theme对象。
import { Theme, ThemeControl } from '@kit.ArkUI'
import { gAppTheme } from './AppTheme'
//在页面build前执行ThemeControl
ThemeControl.setDefaultTheme(gAppTheme)
@Entry
@Component
struct DisplayPage {
@State menuItemColor: ResourceColor = $r('sys.color.background_primary')
onWillApplyTheme(theme: Theme) {
this.menuItemColor = theme.colors.backgroundPrimary;
}
build() {
Column() {
List({ space: 10 }) {
ListItem() {
Column({ space: '5vp' }) {
Text('Color mode')
.margin({ top: '5vp', left: '14fp' })
.width('100%')
Row() {
Column() {
Text('Light')
.fontSize('16fp')
.textAlign(TextAlign.Start)
.alignSelf(ItemAlign.Center)
Radio({ group: 'light or dark', value: 'light' })
.checked(true)
}
.width('50%')
Column() {
Text('Dark')
.fontSize('16fp')
.textAlign(TextAlign.Start)
.alignSelf(ItemAlign.Center)
Radio({ group: 'light or dark', value: 'dark' })
}
.width('50%')
}
}
.width('100%')
.height('90vp')
.borderRadius('10vp')
.backgroundColor(this.menuItemColor)
}
ListItem() {
Column() {
Text('Brightness')
.width('100%')
.margin({ top: '5vp', left: '14fp' })
Slider({ value: 40, max: 100 })
}
.width('100%')
.height('70vp')
.borderRadius('10vp')
.backgroundColor(this.menuItemColor)
}
ListItem() {
Column() {

最低0.47元/天 解锁文章
719

被折叠的 条评论
为什么被折叠?



