HarmonyOS NEXT学习——@Styles、@Extend、stateStyles
创始人
2024-12-26 22:09:01
0

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

  • 仅支持通用属性和通用事件
  • 不支持参数
  • 可以定义全局和组件内使用,全局使用需要加function
// 全局 @Styles function functionName() { ... }  // 在组件内 @Component struct FancyUse {   @Styles fancy() {     .height(100)   } } 
  • 组件内@Styles的优先级高于全局@Styles。
// 定义在全局的@Styles封装的样式 @Styles function globalFancy  () {   .width(100)   .height(100)   .backgroundColor(Color.Pink) }  @Entry @Component @Preview struct FancyUse {   @State heightValue: number = 100   // 定义在组件内的@Styles封装的样式   @Styles fancy() {     .width(200)     .height(this.heightValue)     .backgroundColor(Color.Yellow)     .onClick(() => {       this.heightValue = 200     })   }    build() {     Column({ space: 10 }) {       // 使用全局的@Styles封装的样式       Text('FancyA')         .globalFancy()         .fontSize(30)       // 使用组件内的@Styles封装的样式       Text('FancyB')         .fancy()         .fontSize(30)       //组件内优先级高于全局       Text('FancyAB')         .globalFancy()         .fancy()         .fontSize(30)     }     .alignItems(HorizontalAlign.Start)   } } 

显示效果

@Extend装饰器:定义扩展组件样式

  • @Styles用于样式的扩展
  • 可以传参数
  • 仅能全局定义
@Extend(Text) function fancyText(weightValue: number, color: Color,clickE:()=>void) {   .fontStyle(FontStyle.Italic)   .fontSize(weightValue)   .backgroundColor(color)   .onClick(clickE) } @Entry @Component struct FancyUse {   @State label: string = 'Hello World'    onClickHandler(){     console.log('40')   }   build() {     Column() {       Text(`${this.label}`)         .fancyText(30, Color.Blue,()=>{console.log('30')})       Text(`${this.label}`)         .fancyText(40, Color.Pink,()=>{this.onClickHandler()})       Text(`${this.label}`)         .fancyText(50, Color.Orange,()=>{console.log('50')})     }     .alignItems(HorizontalAlign.Start)   } } 

在这里插入图片描述
三个点击事件
点击事件

stateStyles 多态样式

stateStyles是属性方法,可以根据UI内部状态来设置样式,类似于css伪类,但语法不同。ArkUI提供以下五种状态:

  • focused:获焦态。
  • normal:正常态。
  • pressed:按压态。
  • disabled:不可用态。
  • selected:选中态。
 @Entry @Component struct FancyUse {   @State label: string = 'Hello World'    build() {     Column() {       Text(`${this.label}`)         .stateStyles({           normal:{             .backgroundColor(Color.Yellow)           },           pressed:{             .backgroundColor(Color.Pink)           }         })         .fontSize(40)     }     .alignItems(HorizontalAlign.Start)   } } 

相关内容

热门资讯

透视辅助!鱼扑克德州有没有挂,... 透视辅助!鱼扑克德州有没有挂,wepOKer透视方法(详细辅助辅助教程)1、玩家可以在鱼扑克德州有没...
wpk发牌逻辑!德州之星有辅助... wpk发牌逻辑!德州之星有辅助挂(透视)一贯真的是有挂(详细辅助指导大家)1、完成德州之星有辅助挂透...
hhpoker透视脚本!aa ... hhpoker透视脚本!aa poker透视软件(透视)好像是真的有挂(存在挂教程)1)aa pok...
透视智能ai!wepoker有... 透视智能ai!wepoker有挂吗 百度知道,智星德州可以透视吗(详细辅助可靠技巧);原来确实真的有...
aapoker挂!WPK透视辅... aapoker挂!WPK透视辅助(透视)一向真的有挂(详细辅助玩家分析)1、快速入门:当你通过点击开...
wepoker透视底牌脚本!w... wepoker透视底牌脚本!wepoker俱乐部辅助器(透视)本来真的有挂(揭秘教程)该软件可以轻松...
透视辅助!来玩app德州辅助,... 透视辅助!来玩app德州辅助,HHpoker辅助挂怎么挂(详细辅助透视教程)这是由厦门游乐互动科技有...
微扑克德州专用辅助器!德扑之星... 微扑克德州专用辅助器!德扑之星猫腻(透视)总是存在有挂(详细辅助重大推荐)1、快速入门:当你通过点击...
德普之星辅助器app!hard... 德普之星辅助器app!hardrock作弊(透视)本来存在有挂(介绍教程)1、任何德普之星辅助器ap...
透视工具!德扑胜率 胜率 工具... 透视工具!德扑胜率 胜率 工具,天天欢乐德州可以开挂吗(详细辅助2025新版教程);天天欢乐德州可以...