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

​如何设置对应组件在不同状态下(如无状态、按下、禁用、聚焦、点击),显示不同的样式。

HarmonyOS
2024-02-20 15:18:42
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
rhlee
1

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

示例代码

// xxx.ets 
@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.
分享
微博
QQ
微信
回复
2024-02-20 20:18:47


相关问题
如何设置组件不同状态下样式
2670浏览 • 1回复 待解决
TextInput组件输入状态下隐藏光标
2272浏览 • 1回复 待解决
HarmonyOS 横屏状态下获取组件宽高
1323浏览 • 1回复 待解决
不同组件不同样式效果如何实现
1178浏览 • 1回复 待解决
熄屏状态下网络请求失败
2786浏览 • 1回复 待解决