鸿蒙5.0版开辟:ArkTS组件通用属性(位置设置)

打印 上一主题 下一主题

主题 1583|帖子 1583|积分 4749

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
位置设置

设置组件的对齐方式、布局方向和显示位置。
   阐明:
  从API Version 7开始支持。后续版本如有新增内容,则接纳上角标单独标记该内容的起始版本。
  align

align(value: Alignment)
设置容器元素绘制区域内的子元素的对齐方式。
卡片能力: 从API version 9开始,该接口支持在ArkTS卡片中使用。
原子化服务API: 从API version 11开始,该接口支持在原子化服务中使用。
体系能力: SystemCapability.ArkUI.ArkUI.Full
参数:
参数名范例必填阐明valueAlignment是设置容器元素绘制区域内的子元素的对齐方式。
只在Stack、Button、Marquee、StepperItem、text、TextArea、TextInput、FolderStack中生效,其中和文本相干的组件Marquee、text、TextArea、TextInput的align结果参考textAlign。
不支持textAlign属性的组件则无法设置程度方向的文字对齐。
默认值:Alignment.Center
阐明:
在Stack中该属性与alignContent效果一致,只能设置子组件在容器内的对齐方式。 direction

direction(value: Direction)
设置容器元素内主轴方向上的布局。
卡片能力: 从API version 9开始,该接口支持在ArkTS卡片中使用。
原子化服务API: 从API version 11开始,该接口支持在原子化服务中使用。
体系能力: SystemCapability.ArkUI.ArkUI.Full
参数:
参数名范例必填阐明valueDirection是设置容器元素内主轴方向上的布局。
该属性在Column组件上不生效。
默认值:Direction.Auto position

position(value: Position | Edges | LocalizedEdges)
绝对定位,确定子组件相对父组件的位置。
卡片能力: 从API version 9开始,该接口支持在ArkTS卡片中使用。
原子化服务API: 从API version 11开始,该接口支持在原子化服务中使用。
体系能力: SystemCapability.ArkUI.ArkUI.Full
参数:
参数名范例必填阐明valuePosition | Edges12+ | LocalizedEdges12+是绝对定位,确定子组件相对父组件的位置。当父容器为Row/Column/Flex时,设置position的子组件不占位。
Position范例基于父组件左上角确定位置;Edges范例基于父组件四边确定位置,top/left/right/bottom分别为组件各边距离父组件相应边的边距,通过边距来确定组件相对于父组件的位置;LocalizedEdges范例基于父组件四边确定位置,支持镜像模式。
适用于置顶显示、悬浮按钮等组件在父容器中位置固定的场景。
不支持在宽高为零的布局容器上设置。
当父容器为RelativeContainer, 且子组件设置了alignRules属性, 则子组件的position属性不生效。 markAnchor

markAnchor(value: Position | LocalizedPosition)
设置子元素在位置定位时的锚点。
卡片能力: 从API version 9开始,该接口支持在ArkTS卡片中使用。
原子化服务API: 从API version 11开始,该接口支持在原子化服务中使用。
体系能力: SystemCapability.ArkUI.ArkUI.Full
参数:
参数名范例必填阐明valuePosition | LocalizedPosition12+是设置子元素在位置定位时的锚点,从position或offset的位置上,进一步偏移。
设置.position({x: value1, y: value2}).markAnchor({x: value3, y: value4}),效果等于设置.position({x: value1 - value3, y: value2 - value4}),offset同理。
单独使用markAnchor,设置.markAnchor({x: value1, y: value2}),效果等于设置.offset({x: -value1, y: -value2})。
API version 9及以前,默认值为:
{
x: 0,
y: 0
}
API version 10:无默认值。 offset

offset(value: Position | Edges | LocalizedEdges)
相对偏移,组件相对本来的布局位置举行偏移。
卡片能力: 从API version 9开始,该接口支持在ArkTS卡片中使用。
原子化服务API: 从API version 11开始,该接口支持在原子化服务中使用。
体系能力: SystemCapability.ArkUI.ArkUI.Full
参数:
参数名范例必填阐明valuePosition | Edges12+ | LocalizedEdges12+是相对偏移,组件相对本来的布局位置举行偏移。offset属性不影响父容器布局,仅在绘制时调解位置。
Position范例基于组件自身左上角偏移,Edges范例基于组件自身四边偏移。 offset属性设置 {x: x, y: y} 与设置 {left: x, top: y} 以及 {right: -x, bottom: -y} 效果相同, 范例LocalizedPosition支持镜像模式:LRT模式下start 等同于x,RTL模式下等同于-x
API version 9及以前,默认值为:
{
x: 0,
y: 0
}
API version 10:无默认值。 alignRules9+

alignRules(value: AlignRuleOption)
指定设置在相对容器中子组件的对齐规则,仅当父容器为RelativeContainer时生效。
卡片能力: 从API version 9开始,该接口支持在ArkTS卡片中使用。
原子化服务API: 从API version 11开始,该接口支持在原子化服务中使用。
体系能力: SystemCapability.ArkUI.ArkUI.Full
参数:
参数名范例必填阐明valueAlignRuleOption是指定设置在相对容器中子组件的对齐规则。 AlignRuleOption对象阐明

卡片能力: 从API version 9开始,该接口支持在ArkTS卡片中使用。
名称范例描述left{ anchor: string, align: HorizontalAlign }设置左对齐参数。
- anchor:设置作为锚点的组件的id值。
- align:设置相对于锚点组件的对齐方式。
原子化服务API: 从API version 11开始,该接口支持在原子化服务中使用。right{ anchor: string, align: HorizontalAlign }设置右对齐参数。
- anchor:设置作为锚点的组件的id值。
- align:设置相对于锚点组件的对齐方式。
原子化服务API: 从API version 11开始,该接口支持在原子化服务中使用。middle{ anchor: string, align: HorizontalAlign }设置横向居中对齐方式的参数。
- anchor:设置作为锚点的组件的id值。
- align:设置相对于锚点组件的对齐方式。
原子化服务API: 从API version 11开始,该接口支持在原子化服务中使用。top{ anchor: string, align: VerticalAlign }设置顶部对齐的参数。
- anchor:设置作为锚点的组件的id值。
- align:设置相对于锚点组件的对齐方式。
原子化服务API: 从API version 11开始,该接口支持在原子化服务中使用。bottom{ anchor: string, align: VerticalAlign }设置底部对齐的参数。
- anchor:设置作为锚点的组件的id值。
- align:设置相对于锚点组件的对齐方式。
原子化服务API: 从API version 11开始,该接口支持在原子化服务中使用。center{ anchor: string, align: VerticalAlign }设置纵向居中对齐方式的参数。
原子化服务API: 从API version 11开始,该接口支持在原子化服务中使用。bias11+Bias设置组件在锚点约束下的偏移参数,其值为到左/上侧锚点的距离与锚点间总距离的比值。
卡片能力: 从API version 11开始,该接口支持在ArkTS卡片中使用。
原子化服务API: 从API version 12开始,该接口支持在原子化服务中使用。 Bias对象阐明

原子化服务API: 从API version 12开始,该接口支持在原子化服务中使用。
参数名范例必填阐明horizontalnumber 否程度方向上的bias值。
当子组件的width可以确定并且有2个程度方向的锚点时生效。
默认值: 0.5verticalnumber 否垂直方向上的bias值。
当子组件的height可以确定并且有2个垂直方向的锚点时生效。
默认值: 0.5 chainMode12+

chainMode(direction: Axis, style: ChainStyle)
指定以该组件为链头所构成的链的参数,仅当父容器为RelativeContainer时生效。
体系能力: SystemCapability.ArkUI.ArkUI.Full
参数:
参数名范例必填阐明directionAxis是链的方向。styleChainStyle是链的样式。 示例

示例1

  1. // xxx.ets
  2. @Entry
  3. @Component
  4. struct PositionExample1 {
  5.   build() {
  6.     Column() {
  7.       Column({ space: 10 }) {
  8.         // 元素内容<元素宽高,设置内容在与元素内的对齐方式
  9.         Text('align').fontSize(9).fontColor(0xCCCCCC).width('90%')
  10.         Stack() {
  11.           Text('First show in bottom end').height('65%').backgroundColor(0xD2B48C)
  12.           Text('Second show in bottom end').backgroundColor(0xF5DEB3).opacity(0.9)
  13.         }.width('90%').height(50).margin({ top: 5 }).backgroundColor(0xFFE4C4)
  14.         .align(Alignment.BottomEnd)
  15.         Stack() {
  16.           Text('top start')
  17.         }.width('90%').height(50).margin({ top: 5 }).backgroundColor(0xFFE4C4)
  18.         .align(Alignment.TopStart)
  19.         // 父容器设置direction为Direction.Ltr,子元素从左到右排列
  20.         Text('direction').fontSize(9).fontColor(0xCCCCCC).width('90%')
  21.         Row() {
  22.           Text('1').height(50).width('25%').fontSize(16).backgroundColor(0xF5DEB3)
  23.           Text('2').height(50).width('25%').fontSize(16).backgroundColor(0xD2B48C)
  24.           Text('3').height(50).width('25%').fontSize(16).backgroundColor(0xF5DEB3)
  25.           Text('4').height(50).width('25%').fontSize(16).backgroundColor(0xD2B48C)
  26.         }
  27.         .width('90%')
  28.         .direction(Direction.Ltr)
  29.         // 父容器设置direction为Direction.Rtl,子元素从右到左排列
  30.         Row() {
  31.           Text('1').height(50).width('25%').fontSize(16).backgroundColor(0xF5DEB3).textAlign(TextAlign.End)
  32.           Text('2').height(50).width('25%').fontSize(16).backgroundColor(0xD2B48C).textAlign(TextAlign.End)
  33.           Text('3').height(50).width('25%').fontSize(16).backgroundColor(0xF5DEB3).textAlign(TextAlign.End)
  34.           Text('4').height(50).width('25%').fontSize(16).backgroundColor(0xD2B48C).textAlign(TextAlign.End)
  35.         }
  36.         .width('90%')
  37.         .direction(Direction.Rtl)
  38.       }
  39.     }
  40.     .width('100%').margin({ top: 5 })
  41.   }
  42. }
  43. <strong>ts</strong>
复制代码

示例2

  1. // xxx.ets
  2. @Entry
  3. @Component
  4. struct PositionExample2 {
  5.   build() {
  6.     Column({ space: 20 }) {
  7.       // 设置子组件左上角相对于父组件左上角的偏移位置
  8.       Text('position').fontSize(12).fontColor(0xCCCCCC).width('90%')
  9.       Row() {
  10.         Text('1').size({ width: '30%', height: '50' }).backgroundColor(0xdeb887).border({ width: 1 }).fontSize(16)
  11.           .textAlign(TextAlign.Center)
  12.         Text('2 position(30, 10)')
  13.           .size({ width: '60%', height: '30' })
  14.           .backgroundColor(0xbbb2cb)
  15.           .border({ width: 1 })
  16.           .fontSize(16)
  17.           .align(Alignment.Start)
  18.           .position({ x: 30, y: 10 })
  19.         Text('3').size({ width: '45%', height: '50' }).backgroundColor(0xdeb887).border({ width: 1 }).fontSize(16)
  20.           .textAlign(TextAlign.Center)
  21.         Text('4 position(50%, 70%)')
  22.           .size({ width: '50%', height: '50' })
  23.           .backgroundColor(0xbbb2cb)
  24.           .border({ width: 1 })
  25.           .fontSize(16)
  26.           .position({ x: '50%', y: '70%' })
  27.       }.width('90%').height(100).border({ width: 1, style: BorderStyle.Dashed })
  28.       // 相对于起点偏移,其中x为最终定位点距离起点水平方向间距,x>0往左,反之向右。
  29.       // y为最终定位点距离起点垂直方向间距,y>0向上,反之向下
  30.       Text('markAnchor').fontSize(12).fontColor(0xCCCCCC).width('90%')
  31.       Stack({ alignContent: Alignment.TopStart }) {
  32.         Row()
  33.           .size({ width: '100', height: '100' })
  34.           .backgroundColor(0xdeb887)
  35.         Text('text')
  36.           .fontSize('30px')
  37.           .textAlign(TextAlign.Center)
  38.           .size({ width: 25, height: 25 })
  39.           .backgroundColor(Color.Green)
  40.           .markAnchor({ x: 25, y: 25 })
  41.         Text('text')
  42.           .fontSize('30px')
  43.           .textAlign(TextAlign.Center)
  44.           .size({ width: 25, height: 25 })
  45.           .backgroundColor(Color.Green)
  46.           .markAnchor({ x: -100, y: -25 })
  47.         Text('text')
  48.           .fontSize('30px')
  49.           .textAlign(TextAlign.Center)
  50.           .size({ width: 25, height: 25 })
  51.           .backgroundColor(Color.Green)
  52.           .markAnchor({ x: 25, y: -25 })
  53.       }.margin({ top: 25 }).border({ width: 1, style: BorderStyle.Dashed })
  54.       // 相对定位,x>0向右偏移,反之向左,y>0向下偏移,反之向上
  55.       Text('offset').fontSize(12).fontColor(0xCCCCCC).width('90%')
  56.       Row() {
  57.         Text('1').size({ width: '15%', height: '50' }).backgroundColor(0xdeb887).border({ width: 1 }).fontSize(16)
  58.           .textAlign(TextAlign.Center)
  59.         Text('2  offset(15, 30)')
  60.           .size({ width: 120, height: '50' })
  61.           .backgroundColor(0xbbb2cb)
  62.           .border({ width: 1 })
  63.           .fontSize(16)
  64.           .align(Alignment.Start)
  65.           .offset({ x: 15, y: 30 })
  66.         Text('3').size({ width: '15%', height: '50' }).backgroundColor(0xdeb887).border({ width: 1 }).fontSize(16)
  67.           .textAlign(TextAlign.Center)
  68.         Text('4 offset(-5%, 20%)')
  69.           .size({ width: 100, height: '50' })
  70.           .backgroundColor(0xbbb2cb)
  71.           .border({ width: 1 })
  72.           .fontSize(16)
  73.           .offset({ x: '-5%', y: '20%' })
  74.       }.width('90%').height(100).border({ width: 1, style: BorderStyle.Dashed })
  75.     }
  76.     .width('100%').margin({ top: 25 })
  77.   }
  78. }
  79. <strong>ts</strong>
复制代码

示例3

  1. // xxx.ets
  2. @Entry
  3. @Component
  4. struct Example3 {
  5.   build() {
  6.     Column({ space: 20 }){
  7.       Text('position use Edges').fontSize(12).fontColor(0xCCCCCC).width('90%')
  8.       Row() {
  9.         Text('bottom:0, right:0').size({ width: '30%', height: '50' }).backgroundColor(0xdeb887).border({ width: 1 }).fontSize(16)
  10.           .textAlign(TextAlign.Center).position({bottom: 0, right: 0})
  11.         Text('top:0, left:0').size({ width: '30%', height: '50' }).backgroundColor(0xdeb887).border({ width: 1 }).fontSize(16)
  12.           .textAlign(TextAlign.Center).position({top: 0, left: 0})
  13.         Text('top:10%, left:50%').size({ width: '50%', height: '30' }).backgroundColor(0xbbb2cb).border({ width: 1 }).fontSize(16)
  14.           .textAlign(TextAlign.Center).position({ top: '10%', left: '50%' })
  15.         Text('bottom:0, left:30').size({ width: '50%', height: '30' }).backgroundColor(0xbbb2cb).border({ width: 1 }).fontSize(16)
  16.           .textAlign(TextAlign.Center).position({ bottom: 0, left: 30 })
  17.       }.width('90%').height(100).border({ width: 1, style: BorderStyle.Dashed })
  18.       Text('offset use Edges').fontSize(12).fontColor(0xCCCCCC).width('90%')
  19.       Row() {
  20.         Text('1').size({ width: '25%', height: 50 }).backgroundColor(0xdeb887).border({ width: 1 }).fontSize(16)
  21.           .textAlign(TextAlign.Center)
  22.         Text('2 top:30, left:0').size({ width: '25%', height: 50 }).backgroundColor(0xbbb2cb).border({ width: 1 }).fontSize(16)
  23.           .textAlign(TextAlign.Center).offset({top: 30, left: 0})
  24.         Text('3').size({ width: '25%', height: 50 }).backgroundColor(0xdeb887).border({ width: 1 }).fontSize(16)
  25.           .textAlign(TextAlign.Center)
  26.         Text('4 bottom:10, right:30').size({ width: '25%', height: 50 }).backgroundColor(0xbbb2cb).border({ width: 1 }).fontSize(12)
  27.           .textAlign(TextAlign.Center).offset({bottom: 10, right: 30})
  28.       }.width('90%').height(150).border({ width: 1, style: BorderStyle.Dashed })
  29.     }.width('100%').margin({ top: 25 })
  30.   }
  31. }
  32. <strong>ts</strong>
复制代码

示例4

  1. // xxx.ets
  2. // 通用布局属性支持镜像能力
  3. // 从上到下依次为position,offset,markAnchor接口效果
  4. // 黄色赋值为原本效果,粉色赋值为镜像效果
  5. import { LengthMetrics } from '@ohos.arkui.node';
  6. @Entry
  7. @Component
  8. struct Example4 {
  9.   private scroller: Scroller = new Scroller()
  10.   build() {
  11.     Column() {
  12.       Stack({ alignContent: Alignment.End }) {
  13.         Scroll(this.scroller) {
  14.           Flex({ direction: FlexDirection.Column }) {
  15.             RelativeContainer() {
  16.               Row() {
  17.               }
  18.               .position({ start: LengthMetrics.px(200), top: LengthMetrics.px(100) })
  19.               .width("30%")
  20.               .height("20%")
  21.               .backgroundColor(Color.Pink)
  22.               .padding(50)
  23.               .margin(50)
  24.               Row() {
  25.               }
  26.               .position({ left:'200px', top: '100px' })
  27.               .width("30%")
  28.               .height("20%")
  29.               .backgroundColor(Color.Yellow)
  30.               .padding(50)
  31.               .margin(50)
  32.               Row() {
  33.               }
  34.               .offset({ start: LengthMetrics.vp(100), top: LengthMetrics.vp(200)  })
  35.               .width("30%")
  36.               .height("20%")
  37.               .backgroundColor(Color.Pink)
  38.               .padding(50)
  39.               .margin(50)
  40.               Row() {
  41.               }
  42.               .offset({ left: 100, top: 200  })
  43.               .width("30%")
  44.               .height("20%")
  45.               .backgroundColor(Color.Yellow)
  46.               .padding(50)
  47.               .margin(50)
  48.               Row() {
  49.               }
  50.               .markAnchor({ start: LengthMetrics.fp(100), top: LengthMetrics.fp(-350) })
  51.               .width("30%")
  52.               .height("20%")
  53.               .backgroundColor(Color.Pink)
  54.               .padding(50)
  55.               .margin(50)
  56.               Row() {
  57.               }
  58.               .markAnchor({ x: '100fp', y: '-350fp' })
  59.               .width("30%")
  60.               .height("20%")
  61.               .backgroundColor(Color.Yellow)
  62.               .padding(50)
  63.               .margin(50)
  64.             }
  65.             .backgroundColor(Color.White)
  66.             .padding(50)
  67.             .margin(50)
  68.           }
  69.         }
  70.         .width('100%')
  71.         .scrollBar(BarState.Off)
  72.         .scrollable(ScrollDirection.Vertical)
  73.         ScrollBar({ scroller: this.scroller, direction: ScrollBarDirection.Vertical, state: BarState.Auto }) {
  74.           Text()
  75.             .width(20)
  76.             .height(100)
  77.             .borderRadius(10)
  78.             .backgroundColor('#C0C0C0')
  79.         }.width(20).backgroundColor('#ededed')
  80.       }
  81.     }.height('90%')
  82.   }
  83. }
  84. <strong>ts</strong>
复制代码


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

风雨同行

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表