#鸿蒙通关秘籍#使用stateStyles时如何依据组件的内部状态的不同快速设置不同样式?

HarmonyOS
2024-12-06 14:58:08
3073浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
JS梦织星

这里就要去组件本身上去设置对应的stateStyles属性,具体实现的代码如下:

@Entry
@Component
struct StateStylesSample {
    build() {
        Column() {
            Button('Button1')
                .stateStyles({
                    focused: {
                        .backgroundColor('#ffffeef0')
                    },
                    pressed: {
                        .backgroundColor('#ff707070')
                    },
                    normal: {
                        .backgroundColor('#ff2787d9')
                    }
                })
                .margin(20)
            Button('Button2')
                .stateStyles({
                    focused: {
                        .backgroundColor('#ffffeef0')
                    },
                    pressed: {
                        .backgroundColor('#ff707070')
                    },
                    normal: {
                        .backgroundColor('#ff2787d9')
                    }
                })
                .margin('30%')
        }
    }
}
  • 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.
已于2024-12-18 10:12:03修改
分享
微博
QQ
微信
回复
2024-12-06 16:53:37
相关问题
不同组件不同样式效果如何实现
1181浏览 • 1回复 待解决
如何设置组件不同状态样式
2674浏览 • 1回复 待解决
设置组件不同状态样式
2825浏览 • 1回复 待解决