十六、组件-通用属性-位置设置
位置设置
设置组件对齐方式、布局方向和显示位置。
align:设置元素内容在元素绘制区域内的对齐方式。
direction:设置元素水平方向的布局。
position:基于父容器的定位
markAnchor:相对于自身的定位,x正数代表左移,负数代表右移;y正数代表上移,负数代表下移。
offset:相对于自身的定位,x正数代表右移,负数代表左移;y正数代表下移,负数代表上移。
案例代码:
/** * author:创客未来 * copyright:com.ckFuture.hrb * 组件位置 */ @Entry @Component struct PositionExample { @State message: string = 'Hello World' build() { Column({space:10}) { Column({space:10}) { Text('align:').fontSize(26).fontColor(0x3E3E3E).width('90%') Stack(){ Text('first').fontSize(26).fontColor(0x3E3E3E).height(70).backgroundColor(Color.Orange) Text('second').fontSize(26).fontColor(0x3E3E3E).height(30).backgroundColor(Color.Gray) } .width('90%') .height(100) .backgroundColor(Color.Blue) .align(Alignment.TopStart) } Column({space:10}) { Text('direction:').fontSize(26).fontColor(0x3E3E3E).width('90%') Row(){ Text('1').fontSize(26).fontColor(0x3E3E3E).width('25%').backgroundColor(Color.Blue) Text('2').fontSize(26).fontColor(0x3E3E3E).width('25%').backgroundColor(Color.Brown) Text('3').fontSize(26).fontColor(0x3E3E3E).width('25%').backgroundColor(Color.Green) Text('4').fontSize(26).fontColor(0x3E3E3E).width('25%').backgroundColor(Color.Orange) } .width('90%') .height(100) .backgroundColor(Color.Grey) .direction(Direction.Rtl) } Column({space:10}) { Text('position:').fontSize(26).fontColor(0x3E3E3E).width('90%') Row(){ Text('1').fontSize(26).fontColor(0x3E3E3E).width('25%').backgroundColor(Color.Blue) Text('2').fontSize(26).fontColor(0x3E3E3E).width('25%').backgroundColor(Color.Brown) .position({x:30,y:10}) Text('3').fontSize(26).fontColor(0x3E3E3E).width('25%').backgroundColor(Color.Green) Text('4').fontSize(26).fontColor(0x3E3E3E).width('25%').backgroundColor(Color.Orange) .position({x:'70%',y:60}) } .width('90%') .height(100) .border({width:1}) } Column() { Text('markAnchor:').fontSize(26).fontColor(0x3E3E3E).width('90%') Stack({alignContent:Alignment.TopStart}){ Row(){ } .width(100) .height(100) .backgroundColor(Color.Yellow) Text('1').fontSize(26).fontColor(0x3E3E3E).width('25').height(25).backgroundColor(Color.Blue) .markAnchor({x:25,y:10}) Text('2').fontSize(26).fontColor(0x3E3E3E).width('25').height(25).backgroundColor(Color.Brown) .markAnchor({x:-100,y:-50}) Text('3').fontSize(26).fontColor(0x3E3E3E).width('25').height(25).backgroundColor(Color.Green) .markAnchor({x:25,y:-70}) } } Column({space:10}) { Text('offset:').fontSize(26).fontColor(0x3E3E3E).width('90%') Row(){ Text('1').fontSize(26).fontColor(0x3E3E3E).width('25%').backgroundColor(Color.Blue) Text('2').fontSize(26).fontColor(0x3E3E3E).width('25%').backgroundColor(Color.Brown) .offset({x:30,y:20}) Text('3').fontSize(26).fontColor(0x3E3E3E).width('25%').backgroundColor(Color.Green) Text('4').fontSize(26).fontColor(0x3E3E3E).width('25%').backgroundColor(Color.Orange) .offset({x:-10,y:-10}) } .width('90%') .height(100) .border({width:1}) } } } }
分类:
HarmonyOS
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!