HarmonyOS 如何动态绑定元素的position属性

如何动态绑定元素的position属性

HarmonyOS
2024-12-20 17:03:18
775浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
fox280

将position属性的值定义成变量,通过动态改变变量的值来改变position。

参考样例如下:

@Entry
@Component
struct PositionExample {
  @State posix1 : number = 0;
  @State posiy1 : number = 0;
  @State posix2 : string = '0%';
  @State posiy2 : string = '0%';
  build() {
    Column({ space: 20 }) {
      Text('position').fontSize(12).fontColor(0xCCCCCC).width('90%')
      Column() {
        Text('1').size({ width: '30%', height: '50' }).backgroundColor(0xdeb887).border({ width: 1 }).fontSize(16)
          .textAlign(TextAlign.Center)
        Text('2 position(数字)')
          .size({ width: '60%', height: '30' })
          .backgroundColor(0xbbb2cb)
          .border({ width: 1 })
          .fontSize(16)
          .align(Alignment.Start)
          .position({ x: this.posix1, y: this.posiy1 })
        Text('3 position(百分比)')
          .size({ width: '50%', height: '50' })
          .backgroundColor(0xbbb2cb)
          .border({ width: 1 })
          .fontSize(16)
          .position({ x: this.posix2, y: this.posiy2})
      }.width('90%').height(100).border({ width: 1, style: BorderStyle.Dashed })
      Button('改变posion的值')
        .onClick(() => {
          this.posix1 = 30;
          this.posiy1 = 50;
          this.posix2 = '50%';
          this.posiy2 = '50%';
        })
    }
    .width('100%').margin({ top: 25 })
  }
}
  • 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.
分享
微博
QQ
微信
回复
2024-12-20 19:40:32


相关问题
HarmonyOS 动态隐藏页面元素
808浏览 • 1回复 待解决
如何动态设置组件属性
1420浏览 • 1回复 待解决
HarmonyOS 咨询共享元素动态转场示例
1207浏览 • 1回复 待解决
HarmonyOS如何动态控制控件属性
592浏览 • 0回复 待解决
求js 中 class类名动态绑定问题?
7244浏览 • 1回复 待解决