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)   } } 

相关内容

热门资讯

10分钟辅助挂!搜圈麻将假不假... 10分钟辅助挂!搜圈麻将假不假“详细透视辅助助手教程”原来真的有挂,您好,搜圈麻将假不假这款游戏可以...
记者发布!福建十三水 辅助器(... 记者发布!福建十三水 辅助器(透视)透视辅助神器(2023已更新)(哔哩哔哩);1、福建十三水 辅助...
6分钟实锤!博雅红河棋盘外 挂... 您好,博雅红河棋盘外 挂这款游戏可以开挂的,确实是有挂的,需要了解加微【757446909】很多玩家...
八分钟辅助挂!微乐陕西麻将小程... 八分钟辅助挂!微乐陕西麻将小程序有猫腻吗“详细透视辅助脚本教程”原来真的有挂1、下载好微乐陕西麻将小...
必备科技!多乐够级捕鱼辅助软件... 必备科技!多乐够级捕鱼辅助软件(透视辅助)透明挂透视辅助挂(2023已更新)(哔哩哔哩)1、多乐够级...
让我来分享经验!胖猪竞技有外挂... 让我来分享经验!胖猪竞技有外挂没(辅助)确实存在有挂(2026已更新)(哔哩哔哩)胖猪竞技有外挂没辅...
七分钟攻略!七彩云南游戏有外 ... 七分钟攻略!七彩云南游戏有外 挂吗,wePoke原来真的是有挂,wpk教程(有挂细节)1)七彩云南游...
交流学习经验!老友广东麻将来牌... 交流学习经验!老友广东麻将来牌规律(透视)外挂透视辅助插件(2024已更新)(哔哩哔哩)1、在老友广...
13钟辅助挂!闲来贵州麻将可以... 13钟辅助挂!闲来贵州麻将可以挂吗“详细透视辅助app教程”原来真的有挂是一款可以让一直输的玩家,快...
9分钟攻略!乐乐上海麻将有没有... 9分钟攻略!乐乐上海麻将有没有挂,impoker本来有挂,黑科技教程(有挂教程)乐乐上海麻将有没有挂...