HarmonyOS NEXT实战开发:自定义扩展(设置主题换肤)

概述

对于使用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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值