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

HarmonyOS4.0系列——04、@Styles、@Extend、@Extend事件以及多态样式stateStyles

@Styles、@Extend、@Extend事件以及多态样式stateStyles

@Styles

通用样式
类似于css中的class
语法一:内部样式 放在struct内

  @Styles commonStyle(){.backgroundColor(Color.Pink).padding('20px')}

语法二:外部样式

@Styles function commonStyle() {.backgroundColor(Color.Pink).padding('40px')
}

调用.commonStyle()

总结:@Styles 内部样式和外部样式,内部样式优先级高于外部样式,内部不要需要用函数function定义,外部需要function;
缺点:只能用于通用样式,@Styles不能进行传参

那么如何进行传参呢?

@Extend()

在@Styles的基础上,可以使用@Extend,用于扩展原生组件样式。

@Extend(Text) function textStyle(fs:number){.fontSize(fs).fontColor(Color.Pink)
}

使用规范:和@Styles不同,@Extend仅支持定义在全局,不支持在组件内部定义。

@Extend(Text) function fancy () {.fontColor(Color.Red)
}// superFancyText可以调用预定义的fancy
@Extend(Text) function superFancyText(size:number) {.fontSize(size).fancy()
}

例:

@Entry
@Component
struct ExtendFun {@State message: string = '@Extend'build() {Row() {Column() {// Text(this.message).fontSize(40)Text('Southern Wind').superFancyText(40)}.width('100%')}.height('100%')}}// @Extend(Text) function textStyle(fs:number){
//   .fontSize(fs).fontColor(Color.Pink)
// }@Extend(Text) function fancy () {.fontColor(Color.Red)
}// superFancyText可以调用预定义的fancy
@Extend(Text) function superFancyText(size:number) {.fontSize(size).fancy()
}

效果:

在这里插入图片描述

当然函数与函数直接也是可以传参的

@Entry
@Component
struct ExtendFun {@State message: string = '@Extend'build() {Row() {Column() {// Text(this.message).fontSize(40)Text('Southern Wind').fancy('blue',FontWeight.Bold)Text('HarmonyOS4.0').superFancyText(20)}.width('100%')}.height('100%')}}@Extend(Text) function fancy (color:Color|string,fw:FontWeight) {.fontColor(color).fontWeight(fw)
}// superFancyText可以调用预定义的fancy
@Extend(Text) function superFancyText(size:number) {.fontSize(size).fancy(Color.Red,FontWeight.Bold)
}

在这里插入图片描述

**@Extend(Text)**如果里面是Text就代表对Text标签生效,如果为Button则代表对Button标签生效

@Extend函数事件

声明点击事件

@Extend(Button) function btnFun(click:()=>void) {.fontSize(30).width(150).height(50).onClick(()=>{click()})
}

标签调用:

@Entry
@Component
struct ExtendFun {@State count: number = 0build() {Row() {Column() {Button('点击 ' + this.count).btnFun(()=>{this.count ++})}.width('100%')}.height('100%')}}

效果:
在这里插入图片描述

stateStyles:多态样式

stateStyles可以依据组件的内部状态的不同,快速设置不同样式。其实可以把它理解为一种属性方法,类似于css伪类,但是语法不同

  • focused:获焦态。
  • normal:正常态。
  • pressed:按压态。
  • disabled:不可用态。

演示

@Entry
@Component
struct Index {@State message: string = 'Southern Wind'build() {Row() {Column() {TextInput()Divider().margin(20)TextInput().fontSize(30).fontWeight(FontWeight.Bold).stateStyles({normal:{.backgroundColor(Color.Yellow)},focused:{.backgroundColor(Color.Pink)},pressed:{.backgroundColor(Color.Blue)}})}.width('100%')}.height('100%')}
}
![在这里插入图片描述](https://img-blog.csdnimg.cn/direct/e28693389b8f4ee88bd31ad691a9d783.gif#pic_center)
http://www.lryc.cn/news/269545.html

相关文章:

  • C++项目之酒店客房管理系统架构——设计模式应用场景详解(下)
  • RabbitMQ消息存储JSON格式反序列化
  • Java解决统计有序矩阵中的负数问题
  • 【算法与数据结构】435、LeetCode无重叠区间
  • 【开题报告】基于SpringBoot的茶文化宣传网站设计与实现
  • 用通俗易懂的方式讲解大模型:基于 Langchain 和 ChatChat 部署本地知识库问答系统
  • YOLO训练results.csv文件可视化(原模型与改进模型对比可视化)
  • uni-appcss语法
  • java在线票务系统(选座)Myeclipse开发mysql数据库web结构java编程计算机网页项目
  • Python 简易图形界面库easygui 对话框大全(续)
  • 电容器50ZLH56MEFC6.3X11
  • vscode 支持c,c++编译调试方法
  • MyBatis的缓存!!!!
  • ToB还是ToC?工业级与消费级AR眼镜都能干什么?
  • 设计模式-Java版本
  • 数据库中如何修改和删除字段
  • 在 Golang 应用程序中管理多个数据库
  • 理解开源协议GPL、MIT、BSD、Apache License
  • Talk | 北京大学博士生汪海洋:通向3D感知大模型的前置方案
  • 【C语言数组传参】规则详解
  • 【Linux】Ubuntu22.04版本下实现gcc版本的快速切换
  • 使用Node Exporter采集主机数据
  • Django 文件上传(十二)
  • k8s的陈述式资源管理
  • electron-builder 打包exe后白屏
  • mvvm,vue双向数据绑定的原理
  • 【Java中序列化的原理是什么(解析)】
  • 冠赢互娱基于 OpenKrusieGame 实现游戏云原生架构升级
  • Mybatis 动态 SQL - trim, where, set
  • 大模型系列:OpenAI使用技巧_使用OpenAI进行K-means聚类