如何设置组件不同状态下的样式

如何设置组件不同状态下的样式

HarmonyOS
2024-01-20 11:23:42
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
yeshutang

使用多态样式,在组件的StateStyles接口中,定义组件不同状态下的样式。

代码示例

@Entry 
@Component 
struct StyleExample { 
  @State isEnable: boolean = true; 
 
  @Styles pressedStyles() { 
    .backgroundColor("#ED6F21") 
    .borderRadius(10) 
    .borderStyle(BorderStyle.Dashed) 
    .borderWidth(2) 
    .borderColor('#33000000') 
    .width(120) 
    .height(30) 
    .opacity(1) 
  } 
  build() { 
    Flex({direction: FlexDirection.Column, alignItems: ItemAlign.Center}) { 
      Text("pressed") 
        .backgroundColor('#0A59F7') 
        .borderRadius(20) 
        .borderStyle(BorderStyle.Dotted) 
        .borderWidth(2) 
        .borderColor(Color.Red) 
        .width(100) 
        .height(25) 
        .opacity(1) 
        .fontSize(14) 
        .fontColor(Color.White) 
        .stateStyles({ 
          pressed: this.pressedStyles 
        }) 
        .margin({ 
          bottom: 20 
        }) 
        .textAlign(TextAlign.Center) 
    } 
    .width(350) 
    .height(300) 
  } 
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.

参考链接

多态样式

分享
微博
QQ
微信
回复
2024-01-21 00:33:42
相关问题
设置组件不同状态下样式
2820浏览 • 1回复 待解决
TextInput组件输入状态下隐藏光标
2272浏览 • 1回复 待解决
HarmonyOS 横屏状态下获取组件宽高
1323浏览 • 1回复 待解决
不同组件不同样式效果如何实现
1178浏览 • 1回复 待解决
RichEditor组件如何设置初始字体样式
1650浏览 • 1回复 待解决