概述
对于使用ets声明式前端开发的应用,提供应用内组件的主题能力,支持局部深浅色、动态换肤功能。本功能不支持C-API和Node-API,不支持Ability和窗口的主题设置。
本文提供如下场景:
自定义品牌色
CustomTheme用于自定义主题,属性可选,只需要复写修改的部分,未修改内容继承于系统,请参考:
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() { Row() { Column({ space: '5vp' }) { Text('Touch sensitivity') .fontSize('16fp') .textAlign(TextAlign.Start) .width('100%') Text('Increase the touch sensitivity of your screen' + ' for use with screen protectors') .fontSize('12fp') .fontColor(Color.Blue) .textAlign(TextAlign.Start) .width('100%') } .alignSelf(ItemAlign.Center) .margin({ left: '14fp' }) .width('75%') Toggle({ type: ToggleType.Switch, isOn: true }) .margin({ right: '14fp' }) .alignSelf(ItemAlign.Center) } .width('100%') .height('80vp') } .width('100%') .borderRadius('10vp') .backgroundColor(this.menuItemColor) } } } .padding('10vp') .backgroundColor('#dcdcdc') .width('100%') .height('100%') } } -
在Ability中设置ThemeControl,需要在onWindowStageCreate()方法中setDefaultTheme。

最低0.47元/天 解锁文章
2098

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



