当前位置: 首页 > news >正文

04-鸿蒙4.0学习之样式装饰器相关

04-鸿蒙4.0学习之样式装饰器

@styles装饰器:定义组件重用样式

/*** @styles装饰器:定义组件重用样式*/
@Entry
@Component
struct StyleUI {@State message: string = '@styles'@Styles commonStyle(){.width(200).height(100).backgroundColor(Color.Gray).margin(5)}build() {Row() {Column() {Text(this.message).fontSize(50).fontWeight(FontWeight.Bold).commonStyle()Button().commonStyle()Image('').commonStyle()Row(){}.commonStyle()}.width('100%')}.height('100%')}
}
// 外部通用样式函数 使用function 关键字 内部样式函数 内部不需要function  内部优先级 > 外部样式
// 弊端:只能写通用样式 不能传参
//  @Styles function commonStyle(){
//    .width(200)
//    .height(100)
//    .backgroundColor(Color.Gray)
//   }

@Extend 扩展组件样式

/***@Extend 扩展组件样式*/
@Entry
@Component
struct ExtendFun {@State message: string = '@Extend'@State count: number = 0build() {Row() {Column() {Text(this.message).fontSize(50).fontWeight(FontWeight.Bold)Divider()Text('HarmonyOS4.0').sizeColor(40, Color.Blue)Text('第二行').sizeColor(40, 'red')Text('第三行').textStyle(20, "#6699ff")Text('第四行').textStyle(50, Color.Pink)Button(this.count.toString()).btnStyle(()=>{this.count++})}.width('100%')}.height('100%')}
}@Extend(Text) function sizeColor(fs: number, fc: Color | string) {.fontSize(fs).fontColor(fc)
}@Extend(Text) function textStyle(fs: number, fc: Color | string) {.sizeColor(fs, fc).fontStyle(FontStyle.Italic).fontWeight(FontWeight.Bold)
}@Extend(Button) function btnStyle(click:()=>void) {.fontSize(40).width(150).height(50).onClick(()=>{click()})
}

多态样式

/*** 多态样式*/
@Entry
@Component
struct StateStyleFun {@State message: string = 'stateStyles()'build() {Row() {Column() {Button(this.message).fontSize(50).fontWeight(FontWeight.Bold).stateStyles({normal: {.backgroundColor(Color.Red)},focused: {.backgroundColor(Color.Pink)},pressed: {.backgroundColor(Color.Blue)}})}.width('100%')}.height('100%')}
}
http://www.lryc.cn/news/247750.html

相关文章:

  • C# 线程(1)
  • 冒泡排序以及改进方案
  • QTextEdit 是 Qt 框架中的一个类,用于显示和编辑多行文本内容的可编辑部件
  • vue+jsonp编写可导出html的模版,可通过外部改json动态更新页面内容
  • 查看各ip下的连接数
  • Linux—进程状态
  • 万宾科技可燃气体监测仪科技作用全览
  • Python与GPU编程快速入门(三)
  • praseInt 和 逻辑或连用
  • 对属于国家秘密的地理信息的获取、持有、提供、利用情况进行登记并长期保存,实行可追溯管理
  • XAER_RMERR: Fatal error occurred in the transaction branch异常解决
  • Redis面试常见问题
  • 浏览器触发下载Excel文件-Java实现
  • 每日汇评:黄金在上涨趋势恢复之前面临修正性回调的风险
  • 【开源】基于Vue.js的大学计算机课程管理平台的设计和实现
  • c++环形队列
  • 智能客服核心技术——预测会话与答案生成
  • C语言——计算Fibonacci数列
  • 【ASP.NET CORE】.NET 6.0 NET CORE MVC连接SQLSERVER数据库
  • filebeat日志收集工具
  • 一文例说嵌入式 C 程序的内聚和耦合
  • python-爬虫(可直接使用)
  • Moonbeam生态项目分析 — — 去中心化交易所Beamswap
  • 自研Xilinx高性能PCIe多通道DMA控制器
  • 人工智能原理复习--知识表示(二)
  • 【SpringBoot篇】登录校验 — JWT令牌
  • leetcode每日一题34
  • 王者荣耀游戏制作
  • springboot post添加URL添加参数
  • 『 MySQL数据库 』插入查询结果