HarmonyOS Next开辟学习手册——主题设置(设置主题换肤)

打印 上一主题 下一主题

主题 998|帖子 998|积分 2996

概述

针对应用,构建ArkUI应用级和页面级主题设置本领,并提供局部深淡色模式设置、动态换肤等功能。
本文提供如了局景:


  • 自界说品牌色
  • 应用级自界说品牌色
  • 局部页面自界说主题风格
  • 局部深淡色
自界说品牌色

CustomTheme 用于自界说主题,属性可选,只需要复写修改的部门,未修改内容继承于系统,参考 系统缺省token色值 。请参考:
  1.   import { CustomColors, CustomTheme } from '@kit.ArkUI'
  2.   export class AppColors implements CustomColors {
  3.     //自定义品牌色
  4.     brand: ResourceColor = '#FF75D9';
  5.   }
  6.   export class AppTheme implements CustomTheme {
  7.     public colors: AppColors = new AppColors()
  8.   }
  9.   
  10.   export let gAppTheme: CustomTheme = new AppTheme()
复制代码
设置应用级自界说品牌色



  • 可在页面入口处同一设置,需要在页面build前执行 ThemeControl 。
其中, onWillApplyTheme 回调函数用于自界说组件获取当前生效的Theme对象。
  1.   import { Theme, ThemeControl } from '@kit.ArkUI'
  2.   import { gAppTheme } from './AppTheme'
  3.   
  4.   //在页面build前执行ThemeControl
  5.   ThemeControl.setDefaultTheme(gAppTheme)
  6.   @Entry
  7.   @Component
  8.   struct DisplayPage {
  9.     @State menuItemColor: ResourceColor = $r('sys.color.background_primary')
  10.    
  11.     onWillApplyTheme(theme: Theme) {
  12.       this.menuItemColor = theme.colors.backgroundPrimary;
  13.     }
  14.   
  15.     build() {
  16.       Column() {
  17.         List({ space: 10 }) {
  18.           ListItem() {
  19.             Column({ space: '5vp' }) {
  20.               Text('Color mode')
  21.                 .margin({ top: '5vp', left: '14fp' })
  22.                 .width('100%')
  23.               Row() {
  24.                 Column() {
  25.                   Text('Light')
  26.                     .fontSize('16fp')
  27.                     .textAlign(TextAlign.Start)
  28.                     .alignSelf(ItemAlign.Center)
  29.                   Radio({ group: 'light or dark', value: 'light' })
  30.                     .checked(true)
  31.                 }
  32.                 .width('50%')
  33.                 Column() {
  34.                   Text('Dark')
  35.                     .fontSize('16fp')
  36.                     .textAlign(TextAlign.Start)
  37.                     .alignSelf(ItemAlign.Center)
  38.                   Radio({ group: 'light or dark', value: 'dark' })
  39.                 }
  40.                 .width('50%')
  41.               }
  42.             }
  43.             .width('100%')
  44.             .height('90vp')
  45.             .borderRadius('10vp')
  46.             .backgroundColor(this.menuItemColor)
  47.           }
  48.           ListItem() {
  49.             Column() {
  50.               Text('Brightness')
  51.                 .width('100%')
  52.                 .margin({ top: '5vp', left: '14fp' })
  53.               Slider({ value: 40, max: 100 })
  54.             }
  55.             .width('100%')
  56.             .height('70vp')
  57.             .borderRadius('10vp')
  58.             .backgroundColor(this.menuItemColor)
  59.           }
  60.           ListItem() {
  61.             Column() {
  62.               Row() {
  63.                 Column({ space: '5vp' }) {
  64.                   Text('Touch sensitivity')
  65.                     .fontSize('16fp')
  66.                     .textAlign(TextAlign.Start)
  67.                     .width('100%')
  68.                   Text('Increase the touch sensitivity of your screen' +
  69.                     ' for use with screen protectors')
  70.                     .fontSize('12fp')
  71.                     .fontColor(Color.Blue)
  72.                     .textAlign(TextAlign.Start)
  73.                     .width('100%')
  74.                 }
  75.                 .alignSelf(ItemAlign.Center)
  76.                 .margin({ left: '14fp' })
  77.                 .width('75%')
  78.   
  79.                 Toggle({ type: ToggleType.Switch, isOn: true })
  80.                   .margin({ right: '14fp' })
  81.                   .alignSelf(ItemAlign.Center)
  82.                   .width('25%')
  83.               }
  84.               .width('100%')
  85.               .height('80vp')
  86.             }
  87.             .width('100%')
  88.             .borderRadius('10vp')
  89.             .backgroundColor(this.menuItemColor)
  90.           }
  91.         }
  92.       }
  93.       .padding('10vp')
  94.       .backgroundColor('#dcdcdc')
  95.       .width('100%')
  96.       .height('100%')
  97.     }
  98.   }
复制代码


  • 在Ability中设置 ThemeControl ,需要在onWindowStageCreate()方法中 setDefaultTheme 。
  1.   import AbilityConstant from '@ohos.app.ability.AbilityConstant';
  2.   import hilog from '@ohos.hilog';
  3.   import UIAbility from '@ohos.app.ability.UIAbility';
  4.   import Want from '@ohos.app.ability.Want';
  5.   import window from '@ohos.window';
  6.   import { CustomColors, ThemeControl } from '@kit.ArkUI';
  7.   class AppColors implements CustomColors {
  8.     fontPrimary = 0xFFD53032
  9.     iconOnPrimary = 0xFFD53032
  10.     iconFourth = 0xFFD53032
  11.   }
  12.   
  13.   const abilityThemeColors = new AppColors();
  14.   
  15.   export default class EntryAbility extends UIAbility {
  16.     onCreate(want: Want, launchParam: AbilityConstant.LaunchParam) {
  17.       hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate');
  18.     }
  19.   
  20.     onDestroy() {
  21.       hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy');
  22.     }
  23.   
  24.     onWindowStageCreate(windowStage: window.WindowStage) {
  25.       // Main window is created, set main page for this ability
  26.       hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate');
  27.    
  28.       windowStage.loadContent('pages/Index', (err, data) => {
  29.         if (err.code) {
  30.           hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? '');
  31.           return;
  32.         }
  33.         hilog.info(0x0000, 'testTag', 'Succeeded in loading the content. Data: %{public}s', JSON.stringify(data) ?? '');
  34.         // 在onWindowStageCreate()方法中setDefaultTheme
  35.         ThemeControl.setDefaultTheme({ colors: abilityThemeColors })
  36.         hilog.info(0x0000, 'testTag', '%{public}s', 'ThemeControl.setDefaultTheme done');
  37.       });
  38.     }
  39.   
  40.   }
复制代码

注:假如setDefaultTheme的参数为undefined时,默认token值对应的色值参考 系统缺省token色值 。
设置应用局部页面自界说主题风格

将自界说Theme的配色通过设置 WithTheme 作用于内组件缺省样式,WithTheme作用域内组件配色跟随Theme的配色生效。
在下面示例中,通过WithTheme({ theme: this.myTheme })将作用域内的组件配色设置为自界说主题风格。后续可通过更改this.myTheme更换主题风格。
onWillApplyTheme 回调函数用于自界说组件获取当前生效的Theme对象。
  1.   import { CustomColors, CustomTheme, Theme } from '@kit.ArkUI'
  2.   class AppColors implements CustomColors {
  3.     fontPrimary: ResourceColor = $r('app.color.brand_purple')
  4.     backgroundEmphasize: ResourceColor = $r('app.color.brand_purple')
  5.   }
  6.   
  7.   class AppColorsSec implements CustomColors {
  8.     fontPrimary: ResourceColor = $r('app.color.brand')
  9.     backgroundEmphasize: ResourceColor = $r('app.color.brand')
  10.   }
  11.   
  12.   class AppTheme implements CustomTheme {
  13.     public colors: AppColors = new AppColors()
  14.   }
  15.   
  16.   class AppThemeSec implements CustomTheme {
  17.     public colors: AppColors = new AppColorsSec()
  18.   }
  19.   
  20.   @Entry
  21.   @Component
  22.   struct DisplayPage {
  23.     @State customTheme: CustomTheme = new AppTheme()
  24.     @State message: string = '设置应用局部页面自定义主题风格'
  25.     count = 0;
  26.   
  27.     build() {
  28.       WithTheme({ theme: this.customTheme }) {
  29.         Row(){
  30.           Column() {
  31.             Text('WithTheme')
  32.               .fontSize(30)
  33.               .margin({bottom: 10})
  34.             Text(this.message)
  35.               .margin({bottom: 10})
  36.             Button('change theme').onClick(() => {
  37.               this.count++;
  38.               if (this.count > 1) {
  39.                 this.count = 0;
  40.               }
  41.               switch (this.count) {
  42.                 case 0:
  43.                   this.customTheme = new AppTheme();
  44.                   break;
  45.                 case 1:
  46.                   this.customTheme = new AppThemeSec();
  47.                   break;
  48.               }
  49.             })
  50.           }
  51.           .width('100%')
  52.         }
  53.         .height('100%')
  54.         .width('100%')
  55.       }
  56.     }
  57.   }
复制代码

设置应用页面局部深淡色

通过 WithTheme 可以设置深淡色模式, ThemeColorMode 有三种模式,ThemeColorMode.SYSTEM模式表现跟随系统模式,ThemeColorMode.LIGHT模式表现淡色模式,ThemeColorMode.DARK模式表现深色模式。
在WithTheme作用域内,组件的样式资源取值跟随指定的模式读取对应的深淡色模式系统和应用资源值,WithTheme作用域内的组件配色跟随指定的深浅模式生效。
在下面的示例中,通过WithTheme({ colorMode: ThemeColorMode.DARK })将作用域内的组件设置为深色模式。
设置局部深淡色时,需要在resources文件夹下添加dark目录,dark目录添加dark.json资源文件,深淡色模式才会生效。
  1.   @Entry
  2.   @Component
  3.   struct DisplayPage {
  4.     @State message: string = 'Hello World';
  5.     @State colorMode: ThemeColorMode = ThemeColorMode.DARK;
  6.     build() {
  7.       WithTheme({ colorMode: this.colorMode }) {
  8.         Row() {
  9.           Column() {
  10.             Text(this.message)
  11.               .fontSize(50)
  12.               .fontWeight(FontWeight.Bold)
  13.             Button('Switch ColorMode').onClick(() => {
  14.               if (this.colorMode === ThemeColorMode.LIGHT) {
  15.                 this.colorMode = ThemeColorMode.DARK;
  16.               } else if (this.colorMode === ThemeColorMode.DARK) {
  17.                 this.colorMode = ThemeColorMode.LIGHT;
  18.               }
  19.             })
  20.           }
  21.           .width('100%')
  22.         }
  23.         .backgroundColor($r('sys.color.background_primary'))
  24.         .height('100%')
  25.         .expandSafeArea([SafeAreaType.SYSTEM], [SafeAreaEdge.TOP, SafeAreaEdge.END, SafeAreaEdge.BOTTOM, SafeAreaEdge.START])
  26.       }
  27.     }
  28.   }
复制代码

系统缺省token色值


鸿蒙全栈开辟全新学习指南

有许多小伙伴不知道学习哪些鸿蒙开辟技术?不知道需要重点掌握哪些鸿蒙应用开辟知识点?而且学习时频仍踩坑,最终浪费大量时间。以是要有一份实用的鸿蒙(HarmonyOS NEXT)学习路线与学习文档用来跟着学习是非常有必要的。
针对一些列因素,整理了一套纯血版鸿蒙(HarmonyOS Next)全栈开辟技术的学习路线,包含了鸿蒙开辟必掌握的核心知识要点,内容有(ArkTS、ArkUI开辟组件、Stage模子、多端部署、分布式应用开辟、WebGL、元服务、OpenHarmony多媒体技术、Napi组件、OpenHarmony内核、OpenHarmony驱动开辟、系统定制移植等等)鸿蒙(HarmonyOS NEXT)技术知识点。
本路线共分为四个阶段:

第一阶段:鸿蒙初中级开辟必备技能


第二阶段:鸿蒙南北双向高工技能基础:gitee.com/MNxiaona/733GH


第三阶段:应用开辟中高级就业技术


第四阶段:全网首发-工业级南向装备开辟就业技术:gitee.com/MNxiaona/733GH


《鸿蒙 (Harmony OS)开辟学习手册》(共计892页)

如何快速入门?

1.基本概念
2.构建第一个ArkTS应用
3.……

开辟基础知识:gitee.com/MNxiaona/733GH

1.应用基础知识
2.设置文件
3.应用数据管理
4.应用安全管理
5.应用隐私掩护
6.三方应用调用管控机制
7.资源分类与访问
8.学习ArkTS语言
9.……

基于ArkTS 开辟

1.Ability开辟
2.UI开辟
3.公共事件与关照
4.窗口管理
5.媒体
6.安全
7.网络与链接
8.电话服务
9.数据管理
10.后台任务(Background Task)管理
11.装备管理
12.装备使用信息统计
13.DFX
14.国际化开辟
15.折叠屏系列
16.……

鸿蒙开辟口试真题(含参考答案):gitee.com/MNxiaona/733GH


鸿蒙入门讲授视频:


美团APP实战开辟讲授:gitee.com/MNxiaona/733GH


写在末了



  • 假如你以为这篇内容对你还蛮有资助,我想邀请你帮我三个小忙:
  • 点赞,转发,有你们的 『点赞和批评』,才是我创造的动力。
  • 关注小编,同时可以期待后续文章ing

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

用多少眼泪才能让你相信

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表