【ArkUI】如何调整组件位置

本文介绍了四种调整UI元素在页面中的通用属性:align用于对齐,direction控制行向布局,position实现绝对定位,offset进行相对定位,以及markAnchor设置锚点。这些属性有助于精细调整UI组件的呈现效果。
摘要由CSDN通过智能技术生成

调整位置的通用属性有四种,下面分别介绍如何使用。

方式一:align

设置元素内容在元素绘制区域内的对齐方式。

在这里插入图片描述

Column({space:10}){
  Text("align")
  Stack(){
    Text('内容1').width(80).height(50).backgroundColor(Color.Pink)
    Text('内容2').width(60).height(80).backgroundColor(Color.Black)
  }
  .width('90%')
  .height(120)
  .backgroundColor('#36D')
  .align(Alignment.TopStart)
}

在这里插入图片描述

给Stack组件添加align属性后,Alignment参数可选如下,默认参数为Center

名称描述
TopStart顶部起始「左上角」
Top顶部横向居中
TopEnd顶部尾部「右上角」
Start起始处纵向居中
Center「中心」
End尾部纵向居中
BottomStart底部起始「左下角」
Bottom底部横向居中
BottomEnd底部尾部「右下角」

在这里插入图片描述

方式二:direction

设置元素水平方向的布局。

Column(){
  Text('direction')
  Row(){
    Text('内容1').width('25%').backgroundColor(Color.Pink)
    Text('内容2').width('25%').backgroundColor(Color.Black)
    Text('内容3').width('25%').backgroundColor(Color.Blue)
    Text('内容4').width('25%').backgroundColor(Color.Orange)
  }
  .width('90%')
  .height(120)
  .backgroundColor('#36D')
  .direction(Direction.Rtl)  // 从右到左
}

在这里插入图片描述
参数说明,Auto为默认参数

名称描述
Rtl从右到左
Ltr从左到右
Auto系统默认

方式三:position

绝对定位,设置元素左上角相对于父容器左上角偏移位置。在布局容器中,设置该属性不影响父容器布局,仅在绘制时进行位置调整。

适用于置顶显示、悬浮按钮等组件在父容器中位置固定的场景。

Column(){
  Text('position')
   Row(){
     Text('内容1').width('25%').backgroundColor(Color.Pink)
     Text('内容2').width('25%').backgroundColor(Color.Black).position({x:20,y:10})
     Text('内容3').width('25%').backgroundColor(Color.Blue)
     Text('内容4').width('25%').backgroundColor(Color.Orange).position({x:120,y:100})
   }
   .width('90%')
   .height(120)
   .backgroundColor('#36D')
 }

在这里插入图片描述
在这里插入图片描述

参数为对象,其中x正方向为右,y正方向为下。

方式四:offset

相对定位,设置元素相对于自身的偏移量。设置该属性,不影响父容器布局,仅在绘制时进行位置调整。

Column(){
  Text('offset')
  Row(){
    Text('内容1').width('25%').backgroundColor(Color.Pink)
    Text('内容2').width('25%').backgroundColor(Color.Black).offset({x:20,y:10})
    Text('内容3').width('25%').backgroundColor(Color.Blue)
    Text('内容4').width('25%').backgroundColor(Color.Orange).offset({x:-20,y:'40%'})
  }
  .width('90%')
  .height(120)
  .backgroundColor('#36D')
}

在这里插入图片描述
在这里插入图片描述
参数为对象,其中x正方向为右,y正方向为下。

方式五:markAnchor

设置元素在位置定位时的锚点,以元素左上角作为基准点进行偏移。通常配合position和offset属性使用,单独使用时,效果类似offset

Column({space:10}){
 Text("markAnchor")
  Stack(){
    Text().width(25).height(25).backgroundColor(Color.Pink).markAnchor({x:25,y:25})
    Text().width(25).height(25).backgroundColor(Color.Black).markAnchor({x:-100,y:-100})
  }
  .width(100)
  .height(100)
  .backgroundColor('#36D')
  .align(Alignment.TopStart)
}

在这里插入图片描述
注意:参数为对象,与position和offset不同的是,markAnchor参数中x正方向为 「左」,y正方向为 「上」

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

田本初

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值