W3Cschool
恭喜您成為首批注冊(cè)用戶(hù)
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
設(shè)置組件的對(duì)齊方式、布局方向和顯示位置。
從API Version 7開(kāi)始支持。后續(xù)版本如有新增內(nèi)容,則采用上角標(biāo)單獨(dú)標(biāo)記該內(nèi)容的起始版本。
名稱(chēng) | 參數(shù)類(lèi)型 | 描述 |
---|---|---|
align | 設(shè)置元素內(nèi)容在元素繪制區(qū)域內(nèi)的對(duì)齊方式。 默認(rèn)值:Alignment.Center 從API version 9開(kāi)始,該接口支持在ArkTS卡片中使用。 | |
direction | 設(shè)置元素水平方向的布局。 默認(rèn)值:Direction.Auto 從API version 9開(kāi)始,該接口支持在ArkTS卡片中使用。 | |
position | 絕對(duì)定位,設(shè)置元素左上角相對(duì)于父容器左上角偏移位置。在布局容器中,設(shè)置該屬性不影響父容器布局,僅在繪制時(shí)進(jìn)行位置調(diào)整。 適用于置頂顯示、懸浮按鈕等組件在父容器中位置固定的場(chǎng)景。 | |
markAnchor | 設(shè)置元素在位置定位時(shí)的錨點(diǎn),以元素左上角作為基準(zhǔn)點(diǎn)進(jìn)行偏移。通常配合position和offset屬性使用,單獨(dú)使用時(shí),效果類(lèi)似offset 默認(rèn)值: { x: 0, y: 0 } 從API version 9開(kāi)始,該接口支持在ArkTS卡片中使用。 | |
offset | 相對(duì)定位,設(shè)置元素相對(duì)于自身的偏移量。設(shè)置該屬性,不影響父容器布局,僅在繪制時(shí)進(jìn)行位置調(diào)整。 默認(rèn)值: { x: 0, y: 0 } | |
alignRules9+ | { left?: { anchor: string, align: HorizontalAlign }; right?: { anchor: string, align: HorizontalAlign }; middle?: { anchor: string, align: HorizontalAlign }; top?: { anchor: string, align: VerticalAlign }; bottom?: { anchor: string, align: VerticalAlign }; center?: { anchor: string, align: VerticalAlign } } | 指定相對(duì)容器的對(duì)齊規(guī)則。 - left:設(shè)置左對(duì)齊參數(shù)。 - right:設(shè)置右對(duì)齊參數(shù)。 - middle:設(shè)置橫向居中對(duì)齊方式的參數(shù)。 - top:設(shè)置頂部對(duì)齊的參數(shù)。 - bottom:設(shè)置底部對(duì)齊的參數(shù)。 - center:設(shè)置縱向居中對(duì)齊方式的參數(shù)。 該接口支持在ArkTS卡片中使用。 說(shuō)明: - anchor:設(shè)置作為錨點(diǎn)的組件的id值。 - align:設(shè)置相對(duì)于錨點(diǎn)組件的對(duì)齊方式。 |
- // xxx.ets
- @Entry
- @Component
- struct PositionExample1 {
- build() {
- Column() {
- Column({ space: 10 }) {
- // 元素內(nèi)容<元素寬高,設(shè)置內(nèi)容在與元素內(nèi)的對(duì)齊方式
- Text('align').fontSize(9).fontColor(0xCCCCCC).width('90%')
- Stack() {
- Text('First show in bottom end').height('65%').backgroundColor(0xD2B48C)
- Text('Second show in bottom end').backgroundColor(0xF5DEB3).opacity(0.9)
- }.width('90%').height(50).margin({ top: 5 }).backgroundColor(0xFFE4C4)
- .align(Alignment.BottomEnd)
- Stack() {
- Text('top start')
- }.width('90%').height(50).margin({ top: 5 }).backgroundColor(0xFFE4C4)
- .align(Alignment.TopStart)
- // 父容器設(shè)置direction為Direction.Ltr,子元素從左到右排列
- Text('direction').fontSize(9).fontColor(0xCCCCCC).width('90%')
- Row() {
- Text('1').height(50).width('25%').fontSize(16).backgroundColor(0xF5DEB3)
- Text('2').height(50).width('25%').fontSize(16).backgroundColor(0xD2B48C)
- Text('3').height(50).width('25%').fontSize(16).backgroundColor(0xF5DEB3)
- Text('4').height(50).width('25%').fontSize(16).backgroundColor(0xD2B48C)
- }
- .width('90%')
- .direction(Direction.Ltr)
- // 父容器設(shè)置direction為Direction.Rtl,子元素從右到左排列
- Row() {
- Text('1').height(50).width('25%').fontSize(16).backgroundColor(0xF5DEB3).textAlign(TextAlign.End)
- Text('2').height(50).width('25%').fontSize(16).backgroundColor(0xD2B48C).textAlign(TextAlign.End)
- Text('3').height(50).width('25%').fontSize(16).backgroundColor(0xF5DEB3).textAlign(TextAlign.End)
- Text('4').height(50).width('25%').fontSize(16).backgroundColor(0xD2B48C).textAlign(TextAlign.End)
- }
- .width('90%')
- .direction(Direction.Rtl)
- }
- }
- .width('100%').margin({ top: 5 })
- }
- }
- // xxx.ets
- @Entry
- @Component
- struct PositionExample2 {
- build() {
- Column({ space: 20 }) {
- // 設(shè)置子組件左上角相對(duì)于父組件左上角的偏移位置
- Text('position').fontSize(12).fontColor(0xCCCCCC).width('90%')
- Row() {
- Text('1').size({ width: '30%', height: '50' }).backgroundColor(0xdeb887).border({ width: 1 }).fontSize(16)
- Text('2 position(30, 10)')
- .size({ width: '60%', height: '30' })
- .backgroundColor(0xbbb2cb)
- .border({ width: 1 })
- .fontSize(16)
- .align(Alignment.Start)
- .position({ x: 30, y: 10 })
- Text('3').size({ width: '45%', height: '50' }).backgroundColor(0xdeb887).border({ width: 1 }).fontSize(16)
- Text('4 position(50%, 70%)')
- .size({ width: '50%', height: '50' })
- .backgroundColor(0xbbb2cb)
- .border({ width: 1 })
- .fontSize(16)
- .position({ x: '50%', y: '70%' })
- }.width('90%').height(100).border({ width: 1, style: BorderStyle.Dashed })
- // 相對(duì)于起點(diǎn)偏移,其中x為最終定位點(diǎn)距離起點(diǎn)水平方向間距,x>0往左,反之向右。
- // y為最終定位點(diǎn)距離起點(diǎn)垂直方向間距,y>0向上,反之向下
- Text('markAnchor').fontSize(12).fontColor(0xCCCCCC).width('90%')
- Stack({ alignContent: Alignment.TopStart }) {
- Row()
- .size({ width: '100', height: '100' })
- .backgroundColor(0xdeb887)
- Text('text')
- .size({ width: 25, height: 25 })
- .backgroundColor(Color.Green)
- .markAnchor({ x: 25, y: 25 })
- Text('text')
- .size({ width: 25, height: 25 })
- .backgroundColor(Color.Green)
- .markAnchor({ x: -100, y: -25 })
- Text('text')
- .size({ width: 25, height: 25 })
- .backgroundColor(Color.Green)
- .markAnchor({ x: 25, y: -25 })
- }.margin({ top: 25 }).border({ width: 1, style: BorderStyle.Dashed })
- // 相對(duì)定位,x>0向右偏移,反之向左,y>0向下偏移,反之向上
- Text('offset').fontSize(12).fontColor(0xCCCCCC).width('90%')
- Row() {
- Text('1').size({ width: '15%', height: '50' }).backgroundColor(0xdeb887).border({ width: 1 }).fontSize(16)
- Text('2 offset(15, 30)')
- .size({ width: 120, height: '50' })
- .backgroundColor(0xbbb2cb)
- .border({ width: 1 })
- .fontSize(16)
- .align(Alignment.Start)
- .offset({ x: 15, y: 30 })
- Text('3').size({ width: '15%', height: '50' }).backgroundColor(0xdeb887).border({ width: 1 }).fontSize(16)
- Text('4 offset(-10%, 20%)')
- .size({ width: 100, height: '50' })
- .backgroundColor(0xbbb2cb)
- .border({ width: 1 })
- .fontSize(16)
- .offset({ x: '-5%', y: '20%' })
- }.width('90%').height(100).border({ width: 1, style: BorderStyle.Dashed })
- }
- .width('100%').margin({ top: 25 })
- }
- }
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話(huà):173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: