news 2026/7/22 5:49:18

主题与外观-Cordovaopenharmony多主题切换

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
主题与外观-Cordovaopenharmony多主题切换

一、功能概述

不同用户有不同的审美偏好。"主题与外观"模块提供多种主题选择(如浅色、深色、自动等),让用户可以根据自己的喜好定制应用的外观。本篇文章围绕"主题与外观"展开,介绍如何在Cordova Web 层实现主题切换,并通过OpenHarmony ArkTS 插件提供原生主题应用。

我们继续采用"一段代码一段说明"的写作方式,并包含 ArkTS 示例代码。

二、Web 端主题选择界面

<divid="theme-page"class="page page-theme"><h1>主题与外观</h1><divclass="theme-options"><labelclass="theme-option"><inputtype="radio"name="theme"value="light"/><span>浅色主题</span></label><labelclass="theme-option"><inputtype="radio"name="theme"value="dark"checked/><span>深色主题</span></label><labelclass="theme-option"><inputtype="radio"name="theme"value="auto"/><span>跟随系统</span></label></div><divclass="theme-preview"><h3>预览</h3><divclass="preview-card">这是一个示例卡片</div></div></div>

这段 HTML 定义了主题选择页面的结构。用户可以选择浅色、深色或自动跟随系统主题,下方有一个预览区域展示当前主题效果。

.page-theme{padding:16px 24px;}.theme-options{background:#374151;padding:12px;border-radius:4px;margin-bottom:16px;}.theme-option{display:flex;align-items:center;padding:8px 0;cursor:pointer;}.theme-option input{margin-right:8px;}.theme-preview{background:#374151;padding:12px;border-radius:4px;}.preview-card{background:#1f2937;padding:16px;border-radius:4px;margin-top:8px;}/* 浅色主题 */.theme-light{--bg-primary:#ffffff;--bg-secondary:#f5f5f5;--text-primary:#000000;--text-secondary:#666666;}/* 深色主题 */.theme-dark{--bg-primary:#1f2937;--bg-secondary:#374151;--text-primary:#ffffff;--text-secondary:#cccccc;}

CSS 定义了浅色和深色主题的颜色变量,通过 CSS 变量实现主题切换。

三、主题切换逻辑

asyncfunctionloadTheme(){constsettings=awaitdb.getSettings();consttheme=settings.theme||'dark';document.querySelectorAll('input[name="theme"]').forEach((radio)=>{radio.checked=radio.value===theme;});applyTheme(theme);}functionapplyTheme(theme){document.documentElement.classList.remove('theme-light','theme-dark','theme-auto');if(theme==='auto'){constprefersDark=window.matchMedia('(prefers-color-scheme: dark)').matches;document.documentElement.classList.add(prefersDark?'theme-dark':'theme-light');}else{document.documentElement.classList.add(`theme-${theme}`);}}asyncfunctionsaveTheme(theme){constsettings=awaitdb.getSettings();settings.theme=theme;awaitdb.saveSettings(settings);applyTheme(theme);syncThemeToNative(theme);}

loadTheme从数据库中读取已保存的主题,并应用到页面。applyTheme通过添加 CSS 类来切换主题。saveTheme保存主题选择并同步到原生侧。

document.addEventListener('DOMContentLoaded',()=>{loadTheme();document.querySelectorAll('input[name="theme"]').forEach((radio)=>{radio.addEventListener('change',(e)=>{saveTheme(e.target.value);});});});

DOMContentLoaded时加载主题,并为单选按钮绑定变化事件。

四、通过 Cordova 同步主题到原生层

functionsyncThemeToNative(theme){if(!window.cordova){console.warn('[Theme] cordova not ready, skip native sync');return;}cordova.exec(()=>{console.info('[Theme] sync success');},(err)=>{console.error('[Theme] sync failed',err);},'WaterTrackerTheme','applyTheme',[{theme}]);}

syncThemeToNative将主题选择推送给 ArkTS 插件。

五、OpenHarmony ArkTS 插件与主题管理

// entry/src/main/ets/plugins/WaterTrackerThemePlugin.etsimportcommonfrom'@ohos.app.ability.common';exportinterfaceThemeData{theme:string;}exportclassThemeStore{privatestatic_currentTheme:string='dark';staticsetTheme(theme:string){this._currentTheme=theme;}staticgetcurrentTheme(){returnthis._currentTheme;}}exportdefaultclassWaterTrackerThemePlugin{context:common.UIAbilityContext;constructor(ctx:common.UIAbilityContext){this.context=ctx;}applyTheme(args:Array<Object>,callbackId:number){constdata=args[0]asThemeData;ThemeStore.setTheme(data.theme);console.info(`[ThemePlugin] theme applied:${data.theme}`);}}

ArkTS 侧的WaterTrackerThemePlugin插件接收主题数据,并通过ThemeStore缓存。

六、ArkUI 中应用主题

// entry/src/main/ets/pages/ThemePage.etsimport{ThemeStore}from'../plugins/WaterTrackerThemePlugin';@Componentstruct ThemeView{build(){consttheme=ThemeStore.currentTheme;Column(){Text('当前主题').fontSize(18).margin({bottom:8});Text(`${theme==='light'?'浅色':theme==='dark'?'深色':'自动'}主题`).fontSize(14);}.padding(16)}}

ThemeView组件在原生界面中展示当前主题。

七、小结

本篇文章从主题加载、主题切换、Cordova 桥接到 ArkTS 插件,完整演示了"主题与外观"在 Cordova&openharmony 混合应用中的实现路径。Web 层通过loadTheme加载主题,通过applyTheme应用主题,通过saveTheme保存主题;syncThemeToNative将主题推送给原生侧,ArkTS 侧通过ThemeStoreWaterTrackerThemePlugin缓存数据,ArkUI 组件ThemeView则提供原生展示入口。

通过"一段代码一段说明"的方式,我们把整个主题切换流程拆解得足够细致。你可以在此基础上进一步扩展,例如添加更多主题选项、自定义颜色等功能,让"主题与外观"真正成为用户个性化应用的重要组成部分.

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/7/22 0:59:10

LangFlow中的FAQ自动回答器:企业知识库高效利用

LangFlow中的FAQ自动回答器&#xff1a;企业知识库高效利用 在企业日常运营中&#xff0c;员工和客户常常面临大量重复性问题的咨询——“年假怎么申请&#xff1f;”、“报销流程是什么&#xff1f;”、“产品常见故障如何处理&#xff1f;”……这些问题虽然简单&#xff0c;…

作者头像 李华
网站建设 2026/7/20 13:34:08

Topit终极Mac窗口置顶工具:彻底告别窗口遮挡烦恼

Topit终极Mac窗口置顶工具&#xff1a;彻底告别窗口遮挡烦恼 【免费下载链接】Topit Pin any window to the top of your screen / 在Mac上将你的任何窗口强制置顶 项目地址: https://gitcode.com/gh_mirrors/to/Topit 在当今多任务并行的数字工作环境中&#xff0c;Mac…

作者头像 李华
网站建设 2026/7/20 23:17:38

编写驱动设备函数的用法

从终端输出和文件信息来看&#xff0c;已经编译成功了&#xff08;生成了 RK3568 平台对应的 ARM64 架构驱动模块&#xff09;。一、编译成功的核心依据make过程完成了CC&#xff08;编译&#xff09;、MODPOST&#xff08;模块符号处理&#xff09;、LD&#xff08;链接&#…

作者头像 李华
网站建设 2026/7/21 19:51:58

35、打印服务全解析:设置、故障排除与常见问题解答

打印服务全解析:设置、故障排除与常见问题解答 1. 打印通知设置 在设置打印通知时,可按以下步骤操作: - 打开“Set Notifications (Optional)”向导页面,有两种通知设置方式可供选择: - 邮件通知 :选中“Send Email Notification”复选框,输入一个或多个收件人和发…

作者头像 李华
网站建设 2026/7/21 12:04:01

LangFlow如何管理敏感信息?API密钥加密存储方案

LangFlow 如何安全处理 API 密钥&#xff1f;深入解析其敏感信息管理机制 在 AI 应用开发日益普及的今天&#xff0c;低代码平台正成为开发者快速构建智能工作流的核心工具。LangFlow 作为 LangChain 生态中最具代表性的可视化编排工具&#xff0c;凭借“拖拽即用”的交互体验&…

作者头像 李华