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

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

HarmonyOS
2024-12-20 17:03:18
浏览
收藏 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 })
  }
}
分享
微博
QQ
微信
回复
2024-12-20 19:40:32
相关问题
HarmonyOS 动态隐藏页面元素
407浏览 • 1回复 待解决
如何动态设置组件属性
1090浏览 • 1回复 待解决
HarmonyOS 咨询共享元素动态转场示例
864浏览 • 1回复 待解决
HarmonyOS如何动态控制控件属性
282浏览 • 0回复 待解决
求js 中 class类名动态绑定问题?
6807浏览 • 1回复 待解决