HarmonyOS NEXT应用开发之跨文件样式复用和组件复用

介绍

本示例主要介绍了跨文件样式复用和组件复用的场景。在应用开发中,我们通常需要使用相同功能和样式的ArkUI组件,例如购物页面中会使用相同样式的Button按钮、Text显示文字,我们常用的方法是抽取公共样式或者封装成一个自定义组件到公共组件库中以减少冗余代码。

效果图预览

使用说明

  1. 点击购物车页面的list列表跳转商品详情页。
  2. 两个页面的button组件、text组件、Image等组件复用相同的样式。
  3. 购物车页面使用了自定义封装的Image+Text的图文复合组件。

实现思路

一、跨文件样式复用

适用场景:当需要向外提供单一组件的样式定制效果时,推荐使用这种方案。使用方在调用接口时,编码量相对方式二更少,仅需几行即可完成调用,使用便捷。

  1. 提供方创建AttributeModifier接口的实现类。
// attributeModifier.ets/*自定义class实现Text的AttributeModifier接口
*/
export class CommodityText implements AttributeModifier<TextAttribute> {textType: TextType = TextType.TYPE_ONE;textSize: number = 15;constructor( textType: TextType, textSize: number) {this.textType = textType;this.textSize = textSize;}applyNormalAttribute(instance: TextAttribute): void {if (this.textType === TextType.TYPE_ONE) {instance.fontSize(this.textSize);instance.fontColor($r('app.color.orange'));instance.fontWeight(FontWeight.Bolder);instance.width($r('app.string.max_size'));} else if (this.textType === TextType.TYPE_TWO) {instance.fontSize(this.textSize);instance.fontWeight(FontWeight.Bold);instance.fontColor($r('sys.color.ohos_id_counter_title_font_color'));instance.width($r('app.string.max_size'));} else if (this.textType === TextType.TYPE_Three) {instance.fontColor(Color.Gray);instance.fontSize(this.textSize);instance.fontWeight(FontWeight.Normal);instance.width($r('app.string.max_size'));} else if (this.textType === TextType.TYPE_FOUR) {instance.fontSize(this.textSize);instance.fontColor($r('app.color.orange'));instance.textAlign(TextAlign.Center);instance.border({ width: $r('app.float.float_1'), color: $r('app.color.orange'), style: BorderStyle.Solid });instance.margin({ right: $r('app.float.float_10') });}}
}
/*枚举文本类型
*/
export enum TextType {TYPE_ONE,TYPE_TWO,TYPE_Three,TYPE_FOUR
}
  1. 使用方创建提供方的AttributeModifier实现类实例,并作为系统组件attributeModifier属性方法的参数传入。
// ShoppingCart.ets
import { CommodityText } from '../common/attributeModifier';@Component
export struct Details {// 使用方创建提供方的AttributeModifier实现类实例@State textOne: CommodityText = new CommodityText(TextType.TYPE_ONE, 15);...    build(){...Text($r('app.string.store_name'))// TODO:知识点:将入参的AttributeModifier类实例与系统组件绑定.attributeModifier(this.textOne).fontColor($r('sys.color.ohos_id_counter_title_font_color'))...}
}// Details.ets
import { CommodityText } from '../common/attributeModifier';@Component
export struct Details {// 使用方创建提供方的AttributeModifier实现类实例@State textOne: MyTextModifier = new MyTextModifier(TextType.TYPE_ONE, 30);...    build(){...Text($r('app.string.commodity_price'))// 动态设置组件样式.attributeModifier(this.textOne).width($r('app.float.float_100'))...}
}
二、跨文件组件复用

适用场景:适用于多个原生组件结合的场景,如Image+Text等复合自定义组件。

  1. 提供方在公共组件库中创建公用的自定义组件,该组件支持外部传入attributeModifier属性。
//CommonText.ets/*** 自定义封装图文组件*/
@Component
export struct ImageText {@State item: string | Resource = $r('app.string.text');@State textOneContent: string | Resource = $r('app.string.text');@State textTwoContent: string | Resource = $r('app.string.text');@State textThreeContent: string | Resource = $r('app.string.text');@State imageSrc: PixelMap | ResourceStr | DrawableDescriptor = $r('app.media.icon');// TODO:知识点:接受外部传入的AttributeModifier类实例,可以只定制部分组件,选择性传入参数。@State textOne: AttributeModifier<TextAttribute> = new TextModifier();@State textTwo: AttributeModifier<TextAttribute> = new TextModifier();@State textThree: AttributeModifier<TextAttribute> = new TextModifier();@State imageModifier: AttributeModifier<ImageAttribute> = new ImageModifier();@State checkboxModifier: AttributeModifier<CheckboxAttribute> = new CheckboxModifier();build() {Row() {Row() {Checkbox().attributeModifier(this.checkboxModifier)// TODO:知识点:AttributeModifier不支持入参为CustomBuilder或Lamda表达式的属性,且不支持事件和手势。image只能单独通过入参传递使用。Image(this.imageSrc).attributeModifier(this.imageModifier)}.margin({ right: $r('app.float.float_10'), bottom: $r('app.float.float_15') })Column({ space: COLUMN_SPACE }) {// TODO:知识点:将入参的AttributeModifier类实例与系统组件绑定Text(this.item).attributeModifier(this.textTwo)Text(this.textThreeContent).attributeModifier(this.textThree)CommonText({ textFour: new TextModifier() })Text(this.textOneContent).attributeModifier(this.textOne).fontColor($r('app.color.orange'))}}.padding({ top: $r('app.float.float_5') }).width($r('app.string.max_size')).height($r('app.string.max_size'))}
}/*自定义class实现image的AttributeModifier接口,用于初始化
*/
class ImageModifier implements AttributeModifier<ImageAttribute> {applyNormalAttribute(instance: ImageAttribute): void {instance.width($r('app.float.float_100'));instance.height($r('app.float.float_100'));}
}/*自定义class实现text的AttributeModifier接口,用于初始化
*/
class TextModifier implements AttributeModifier<TextAttribute> {applyNormalAttribute(instance: TextAttribute): void {instance.fontSize($r('app.float.float_12'));instance.fontColor($r('app.color.orange'));instance.textAlign(TextAlign.Center);instance.border({ width: $r('app.float.float_1'), color: $r('app.color.orange'), style: BorderStyle.Solid });instance.margin({ right: $r('app.float.float_10') });}
}/*自定义class实现checkbox的AttributeModifier接口,用于初始化
*/
class CheckboxModifier implements AttributeModifier<CheckboxAttribute> {applyNormalAttribute(instance: CheckboxAttribute): void {instance.width($r('app.float.float_15'));instance.height($r('app.float.float_15'));}
}
  1. 使用方分别实现Image组件和Text组件的AttributeModifier接口实现类。
/*自定义class实现Image组件的AttributeModifier接口
*/
export class ImageModifier implements AttributeModifier<ImageAttribute> {width: Length | Resource = 0;height: Length | Resource = 0;constructor(width: Length | Resource, height: Length | Resource) {this.width = width;this.height = height;}applyNormalAttribute(instance: ImageAttribute): void {instance.width(this.width);instance.height(this.height);instance.borderRadius($r('app.float.float_10'));}
}/*自定义class实现Text的AttributeModifier接口
*/
export class CommodityText implements AttributeModifier<TextAttribute> {...
}
  1. 使用方创建Image组件和Text组件的AttributeModifier接口实现类实例,并作为提供方自定义组件CustomImageText的入参传入。
@Component
export struct ShoppingCart {// TODO:知识点:使用方创建Image组件和Text组件的AttributeModifier接口实现类实例@State textOne: CommodityText = new CommodityText(TextType.TYPE_ONE, 15);@State textTwo: CommodityText = new CommodityText(TextType.TYPE_TWO, 17);@State textThree: CommodityText = new CommodityText(TextType.TYPE_Three, 15);@State imageModifier: ImageModifier = new ImageModifier(100, 100);@State checkboxModifier: CheckboxModifier = new CheckboxModifier();build() {...// AttributeModifier实例作为提供方自定义组件ImageText的入参传入。ImageText({item: item,textOne: this.textOne,textTwo: this.textTwo,textThree: this.textThree,imageModifier: this.imageModifier,imageSrc: $r('app.media.icon'),checkboxModifier: this.checkboxModifier,textOneContent: $r('app.string.commodity_price'),textTwoContent: $r('app.string.commodity_name'),textThreeContent: $r('app.string.commodity_model')})... }
}

高性能知识点

本示例使用了动态属性设置和自定义封装公共组件,实现了跨文件样式和组件复用,减少了工程很多冗余的代码。

工程结构&模块类型

dynamicattributes
|---common
|   |---AttributeModifier.ets               // 自定义AttributeModifier接口
|   |---CommonText.ets                      // 自定义组件封装
|   |---LazyForEach.ets                     // 懒加载
|---pages
|   |---ShoppingCart.ets                    // 页面一:购物车
|   |---Details.ets                         // 页面二:详情页

模块依赖

本示例依赖路由管理模块。

参考资料

动态属性设置

ArkUI组件封装及复用场景介绍

为了能让大家更好的学习鸿蒙(HarmonyOS NEXT)开发技术,这边特意整理了《鸿蒙开发学习手册》(共计890页),希望对大家有所帮助:https://qr21.cn/FV7h05

《鸿蒙开发学习手册》:

如何快速入门:https://qr21.cn/FV7h05

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

开发基础知识:https://qr21.cn/FV7h05

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

基于ArkTS 开发:https://qr21.cn/FV7h05

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

鸿蒙开发面试真题(含参考答案):https://qr18.cn/F781PH

鸿蒙开发面试大盘集篇(共计319页):https://qr18.cn/F781PH

1.项目开发必备面试题
2.性能优化方向
3.架构方向
4.鸿蒙开发系统底层方向
5.鸿蒙音视频开发方向
6.鸿蒙车载开发方向
7.鸿蒙南向开发方向

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/761388.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

力扣4寻找两个正序数组的中位数

1.实验内容 给定两个大小分别为 m 和 n 的正序&#xff08;从小到大&#xff09;数组 nums1 和 nums2。请你找出并返回这两个正序数组的 中位数 。 2.实验目的 算法的时间复杂度应该为 O(log (mn)) 。 3.基本思路 碰到时间复杂度要求log的&#xff0c;肯定用二分查找&…

【Markdown】【mermaid】Mermaid流程图基础语法Flowcharts - Basic Syntax

流程图 - 基础语法 流程图简介节点默认节点带文本的节点Unicode 文本Markdown 格式化 流程图方向节点形状圆角矩形节点体育场形节点子程序形节点圆柱形节点圆形节点不对称节点菱形节点六边形节点平行四边形节点另一种平行四边形节点梯形节点另一种梯形节点双圆节点 节点之间的连…

shell编程入门(笔记)

1、shell编程基础&#xff1a; 1.1、shell的解释执行功能 1.2、什么是shell程序&#xff1f; 1.3、shell程序编程的主要内容 1.4、shell程序的第一行 1.5、变量要求 1.6、环境变量和只读变量 1.7、位置参量 1.8、位置参量列表 1.9、数组 2、输入输出 2.1、输入-read命令 2.2…

Pytest用例间参数传递的两种实现方式示例

前言 我们在做接口自动化测试的时候&#xff0c;会经常遇到这种场景&#xff1a;接口A的返回结果中的某个字段&#xff0c;是接口B的某个字段的入参。如果是使用postman&#xff0c;那我们可以通过设置后置变量&#xff0c;然后在需要使用的地方通过{{}}的方式来进行调用。但是…

[LLM]大模型基础知识点--大模型与LLM

1、什么是大模型&#xff1f; 定义&#xff1a;在机器学习领域&#xff0c;"大模型"通常指的是拥有大量参数的深度学习模型。这些模型通常由数十亿甚至数千亿个参数组成。 特点&#xff1a;由于参数众多&#xff0c;这些模型能够从大量数据中学习复杂的模式和关系。…

OJ : 1087 : 获取出生日期(多实例测试)

题目描述 输入某人的18位身份证号&#xff0c;输出其出生日期。 输入 多实例测试。首先输入一个整数n&#xff0c;表示测试实例的个数&#xff0c;然后是n行&#xff0c;每行是一个18位身份证号。 输出 对于输入的每个身份证号&#xff0c;输出一行&#xff0c;即其对应的…

SpringMVC 的运行流程

Spring MVC 是一个基于 MVC 设计模式的 Web 框架&#xff0c;它提供了一种优雅的方式来构建 Web 应用程序&#xff0c;将应用程序的不同部分分离开来&#xff0c;以便更好地管理和维护。了解 Spring MVC 的运行流程对于理解它的工作原理以及在实际项目中的应用非常重要。 1. 请…

银行卡账户交易异常已被限制部分功能,怎么办?

文章目录 I 解决方案1.1 限制原因1.2 防范1.3 案例1.4 用卡安全小知识II 个人账户收款监管规则III 反诈提醒I 解决方案 处理非柜面交易限制,只能到开户行柜台申请解除。异地卡的,需要联系开户行,提供相关资料。有些地方银行的,比如长沙银行,可以使用线上柜台进行审核。先到…

# termux连接云服务器

termux使用 pkg install openssh 连接服务器 ssh root39.100.181.23 安装postgres

不满足软件包要求‘transformers==4.30.2‘, ‘sse-starlette

transformers4.30.2支持的SSE-Starlette版本是0.14.0

Flume入门概述及安装部署

目录 一、Flume概述1.1 Flume定义1.2 Flume基础架构 二、Flume安装部署 一、Flume概述 1.1 Flume定义 Flume是Cloudera提供的一个高可用的&#xff0c;高可靠的&#xff0c;分布式的海量日志采集、聚合和传输的系统。Flume基于流式架构&#xff0c;灵活简单。 1.2 Flume基础…

Python实现计算复读次数(附带源码)

在Python中&#xff0c;可以使用以下几种方法计算复读次数&#xff1a; 方法一&#xff1a;使用count()函数 count()函数用于统计字符串中某个字符或子字符串出现的次数。 示例&#xff1a; Python # 计算字符串中 "a" 出现的次数 count "hello world&quo…

13、Deconstructing Denoising Diffusion Models for Self-Supervised Learning

简介 研究了最初用于图像生成的去噪扩散模型(DDM)的表示学习能力 解构DDM&#xff0c;逐步将其转变为经典的去噪自动编码器(DAE) 探索现代ddm的各个组成部分如何影响自监督表征学习 结论&#xff1a; 只有很少的现代组件对于学习良好的表示是至关重要的&#xff0c;而其他许多…

Linux gcc二进制下载地址

Index of /download/x86_64/releases 待验证&#xff1a; export PATH$HOME/YOURS/gcc-5.4.0/bin:$PATH export LD_LIBRARY_PATH$HOME/YOURS/gcc-5.4.0/lib:$HOME/YOURS/gcc-5.4.0/lib64:$LD_LIBRARY_PATH

【算法刷题day2】Leetcode:977.有序数组的平方、 209.长度最小的子数组、59.螺旋矩阵II

Leetcode:977.有序数组的平方 文档讲解&#xff1a;[代码随想录] 题目链接&#xff1a;977.有序数组的平方 状态&#xff1a;ok 题目&#xff1a; 给你一个按 非递减顺序 排序的整数数组 nums&#xff0c;返回 每个数字的平方 组成的新数组&#xff0c;要求也按 非递减顺序 排序…

web蓝桥杯真题:心愿便利贴

代码及注释&#xff1a; <!-- TODO 待修改的代码 --> <div class"card" :class"item.css" v-for"(item,index) in wishList" :key"index"><div class"header"><img class"close" click&quo…

智能ai写作神器,推荐5个ai在线写作生成器

智能AI写作神器&#xff0c;是不是听起来就很神奇&#xff1f;它们可以帮助我们省去无尽的头疼和煎熬&#xff0c;让我们的文字轻松流畅&#xff0c;幽默风趣。今天&#xff0c;我要向大家推荐五款AI在线写作生成器&#xff0c;让我们一起来看看吧&#xff01; 第一个&#xff…

训练YOLOv9-S

1. YOLOv9-S网络结构 1.1 改前改后的网络结构&#xff08;参数量、计算量&#xff09;对比 修改前调用的yolo.py测试的yolov9.yaml的打印网络情况&#xff0c;包含参数量、计算量 修改后调用的yolo.py测试的yolov9.yaml的打印网络情况&#xff0c;包含参数量、计算量 1.2 …

flutter实现视频播放器,可根据指定视频地址播放、设置声音,进度条拖动,下载等

需要装依赖&#xff1a; gallery_saver: ^2.3.2video_player: ^2.8.3 实现代码 import dart:async; import dart:io;import package:flutter/material.dart; import package:gallery_saver/gallery_saver.dart; import package:path_provider/path_provider.dart; import pac…

Revit2020也能玩衍生式设计?

Revit2021新增的一个好玩功能就是衍生式设计&#xff0c;但是Autodesk2021系列的激活目前还比较麻烦&#xff0c;尤其是要装多款2021软件的时候&#xff0c;注册机用起来还挺烦人的&#xff0c;于是&#xff0c;为了省事&#xff0c;我把GenerativeDesignRevit节点包扔到了Dyna…