W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗(yàn)值獎勵
線性布局(LinearLayout)是開發(fā)中最常用的布局,通過線性容器Row和Column構(gòu)建。線性布局是其他布局的基礎(chǔ),其子元素在線性方向上(水平方向和垂直方向)依次排列。線性布局的排列方向由所選容器組件決定,Column容器內(nèi)子元素按照垂直方向排列,Row容器內(nèi)子元素按照水平方向排列。根據(jù)不同的排列方向,開發(fā)者可選擇使用Row或Column容器創(chuàng)建線性布局。
- Column({ space: 20 }) {
- Text('space: 20').fontSize(15).fontColor(Color.Gray).width('90%')
- Row().width('90%').height(50).backgroundColor(0xF5DEB3)
- Row().width('90%').height(50).backgroundColor(0xD2B48C)
- Row().width('90%').height(50).backgroundColor(0xF5DEB3)
- }.width('100%')
- Row({ space: 35 }) {
- Text('space: 35').fontSize(15).fontColor(Color.Gray)
- Row().width('10%').height(150).backgroundColor(0xF5DEB3)
- Row().width('10%').height(150).backgroundColor(0xD2B48C)
- Row().width('10%').height(150).backgroundColor(0xF5DEB3)
- }.width('90%')
在布局容器內(nèi),可以通過alignItems屬性設(shè)置子元素在交叉軸(排列方向的垂直方向)上的對齊方式。且在各類尺寸屏幕中,表現(xiàn)一致。其中,交叉軸為垂直方向時,取值為VerticalAlign類型,水平方向取值為HorizontalAlign。
alignSelf屬性用于控制單個子元素在容器交叉軸上的對齊方式,其優(yōu)先級高于alignItems屬性,如果設(shè)置了alignSelf屬性,則在單個子元素上會覆蓋alignItems屬性。
- Column({}) {
- Column() {
- }.width('80%').height(50).backgroundColor(0xF5DEB3)
- Column() {
- }.width('80%').height(50).backgroundColor(0xD2B48C)
- Column() {
- }.width('80%').height(50).backgroundColor(0xF5DEB3)
- }.width('100%').alignItems(HorizontalAlign.Start).backgroundColor('rgb(242,242,242)')
- Column({}) {
- Column() {
- }.width('80%').height(50).backgroundColor(0xF5DEB3)
- Column() {
- }.width('80%').height(50).backgroundColor(0xD2B48C)
- Column() {
- }.width('80%').height(50).backgroundColor(0xF5DEB3)
- }.width('100%').alignItems(HorizontalAlign.Center).backgroundColor('rgb(242,242,242)')
- Column({}) {
- Column() {
- }.width('80%').height(50).backgroundColor(0xF5DEB3)
- Column() {
- }.width('80%').height(50).backgroundColor(0xD2B48C)
- Column() {
- }.width('80%').height(50).backgroundColor(0xF5DEB3)
- }.width('100%').alignItems(HorizontalAlign.End).backgroundColor('rgb(242,242,242)')
- Row({}) {
- Column() {
- }.width('20%').height(30).backgroundColor(0xF5DEB3)
- Column() {
- }.width('20%').height(30).backgroundColor(0xD2B48C)
- Column() {
- }.width('20%').height(30).backgroundColor(0xF5DEB3)
- }.width('100%').height(200).alignItems(VerticalAlign.Top).backgroundColor('rgb(242,242,242)')
- Row({}) {
- Column() {
- }.width('20%').height(30).backgroundColor(0xF5DEB3)
- Column() {
- }.width('20%').height(30).backgroundColor(0xD2B48C)
- Column() {
- }.width('20%').height(30).backgroundColor(0xF5DEB3)
- }.width('100%').height(200).alignItems(VerticalAlign.Center).backgroundColor('rgb(242,242,242)')
- Row({}) {
- Column() {
- }.width('20%').height(30).backgroundColor(0xF5DEB3)
- Column() {
- }.width('20%').height(30).backgroundColor(0xD2B48C)
- Column() {
- }.width('20%').height(30).backgroundColor(0xF5DEB3)
- }.width('100%').height(200).alignItems(VerticalAlign.Bottom).backgroundColor('rgb(242,242,242)')
在布局容器內(nèi),可以通過justifyContent屬性設(shè)置子元素在容器主軸上的排列方式??梢詮闹鬏S起始位置開始排布,也可以從主軸結(jié)束位置開始排布,或者均勻分割主軸的空間。
- Column({}) {
- Column() {
- }.width('80%').height(50).backgroundColor(0xF5DEB3)
- Column() {
- }.width('80%').height(50).backgroundColor(0xD2B48C)
- Column() {
- }.width('80%').height(50).backgroundColor(0xF5DEB3)
- }.width('100%').height(300).backgroundColor('rgb(242,242,242)').justifyContent(FlexAlign.Start)
- Column({}) {
- Column() {
- }.width('80%').height(50).backgroundColor(0xF5DEB3)
- Column() {
- }.width('80%').height(50).backgroundColor(0xD2B48C)
- Column() {
- }.width('80%').height(50).backgroundColor(0xF5DEB3)
- }.width('100%').height(300).backgroundColor('rgb(242,242,242)').justifyContent(FlexAlign.Center)
- Column({}) {
- Column() {
- }.width('80%').height(50).backgroundColor(0xF5DEB3)
- Column() {
- }.width('80%').height(50).backgroundColor(0xD2B48C)
- Column() {
- }.width('80%').height(50).backgroundColor(0xF5DEB3)
- }.width('100%').height(300).backgroundColor('rgb(242,242,242)').justifyContent(FlexAlign.End)
- Column({}) {
- Column() {
- }.width('80%').height(50).backgroundColor(0xF5DEB3)
- Column() {
- }.width('80%').height(50).backgroundColor(0xD2B48C)
- Column() {
- }.width('80%').height(50).backgroundColor(0xF5DEB3)
- }.width('100%').height(300).backgroundColor('rgb(242,242,242)').justifyContent(FlexAlign.SpaceBetween)
- Column({}) {
- Column() {
- }.width('80%').height(50).backgroundColor(0xF5DEB3)
- Column() {
- }.width('80%').height(50).backgroundColor(0xD2B48C)
- Column() {
- }.width('80%').height(50).backgroundColor(0xF5DEB3)
- }.width('100%').height(300).backgroundColor('rgb(242,242,242)').justifyContent(FlexAlign.SpaceAround)
- Column({}) {
- Column() {
- }.width('80%').height(50).backgroundColor(0xF5DEB3)
- Column() {
- }.width('80%').height(50).backgroundColor(0xD2B48C)
- Column() {
- }.width('80%').height(50).backgroundColor(0xF5DEB3)
- }.width('100%').height(300).backgroundColor('rgb(242,242,242)').justifyContent(FlexAlign.SpaceEvenly)
- Row({}) {
- Column() {
- }.width('20%').height(30).backgroundColor(0xF5DEB3)
- Column() {
- }.width('20%').height(30).backgroundColor(0xD2B48C)
- Column() {
- }.width('20%').height(30).backgroundColor(0xF5DEB3)
- }.width('100%').height(200).backgroundColor('rgb(242,242,242)').justifyContent(FlexAlign.Start)
- Row({}) {
- Column() {
- }.width('20%').height(30).backgroundColor(0xF5DEB3)
- Column() {
- }.width('20%').height(30).backgroundColor(0xD2B48C)
- Column() {
- }.width('20%').height(30).backgroundColor(0xF5DEB3)
- }.width('100%').height(200).backgroundColor('rgb(242,242,242)').justifyContent(FlexAlign.Center)
- Row({}) {
- Column() {
- }.width('20%').height(30).backgroundColor(0xF5DEB3)
- Column() {
- }.width('20%').height(30).backgroundColor(0xD2B48C)
- Column() {
- }.width('20%').height(30).backgroundColor(0xF5DEB3)
- }.width('100%').height(200).backgroundColor('rgb(242,242,242)').justifyContent(FlexAlign.End)
- Row({}) {
- Column() {
- }.width('20%').height(30).backgroundColor(0xF5DEB3)
- Column() {
- }.width('20%').height(30).backgroundColor(0xD2B48C)
- Column() {
- }.width('20%').height(30).backgroundColor(0xF5DEB3)
- }.width('100%').height(200).backgroundColor('rgb(242,242,242)').justifyContent(FlexAlign.SpaceBetween)
- Row({}) {
- Column() {
- }.width('20%').height(30).backgroundColor(0xF5DEB3)
- Column() {
- }.width('20%').height(30).backgroundColor(0xD2B48C)
- Column() {
- }.width('20%').height(30).backgroundColor(0xF5DEB3)
- }.width('100%').height(200).backgroundColor('rgb(242,242,242)').justifyContent(FlexAlign.SpaceAround)
- Row({}) {
- Column() {
- }.width('20%').height(30).backgroundColor(0xF5DEB3)
- Column() {
- }.width('20%').height(30).backgroundColor(0xD2B48C)
- Column() {
- }.width('20%').height(30).backgroundColor(0xF5DEB3)
- }.width('100%').height(200).backgroundColor('rgb(242,242,242)').justifyContent(FlexAlign.SpaceEvenly)
在線性布局下,常用空白填充組件Blank,在容器主軸方向自動填充空白空間,達(dá)到自適應(yīng)拉伸效果。Row和Column作為容器,只需要添加寬高為百分比,當(dāng)屏幕寬高發(fā)生變化時,會產(chǎn)生自適應(yīng)效果。
- @Entry
- @Component
- struct BlankExample {
- build() {
- Column() {
- Row() {
- Text('Bluetooth').fontSize(18)
- Blank()
- Toggle({ type: ToggleType.Switch, isOn: true })
- }.backgroundColor(0xFFFFFF).borderRadius(15).padding({ left: 12 }).width('100%')
- }.backgroundColor(0xEFEFEF).padding(20).width('100%')
- }
- }
自適應(yīng)縮放是指子組件隨容器尺寸的變化而按照預(yù)設(shè)的比例自動調(diào)整尺寸,適應(yīng)各種不同大小的設(shè)備。在線性布局中,可以使用以下兩種方法實(shí)現(xiàn)自適應(yīng)縮放。
- @Entry
- @Component
- struct layoutWeightExample {
- build() {
- Column() {
- Text('1:2:3').width('100%')
- Row() {
- Column() {
- Text('layoutWeight(1)')
- .textAlign(TextAlign.Center)
- }.layoutWeight(1).backgroundColor(0xF5DEB3).height('100%')
- Column() {
- Text('layoutWeight(2)')
- .textAlign(TextAlign.Center)
- }.layoutWeight(2).backgroundColor(0xD2B48C).height('100%')
- Column() {
- Text('layoutWeight(3)')
- .textAlign(TextAlign.Center)
- }.layoutWeight(3).backgroundColor(0xF5DEB3).height('100%')
- }.backgroundColor(0xffd306).height('30%')
- Text('2:5:3').width('100%')
- Row() {
- Column() {
- Text('layoutWeight(2)')
- .textAlign(TextAlign.Center)
- }.layoutWeight(2).backgroundColor(0xF5DEB3).height('100%')
- Column() {
- Text('layoutWeight(5)')
- .textAlign(TextAlign.Center)
- }.layoutWeight(5).backgroundColor(0xD2B48C).height('100%')
- Column() {
- Text('layoutWeight(3)')
- .textAlign(TextAlign.Center)
- }.layoutWeight(3).backgroundColor(0xF5DEB3).height('100%')
- }.backgroundColor(0xffd306).height('30%')
- }
- }
- }
- @Entry
- @Component
- struct WidthExample {
- build() {
- Column() {
- Row() {
- Column() {
- Text('left width 20%')
- .textAlign(TextAlign.Center)
- }.width('20%').backgroundColor(0xF5DEB3).height('100%')
- Column() {
- Text('center width 50%')
- .textAlign(TextAlign.Center)
- }.width('50%').backgroundColor(0xD2B48C).height('100%')
- Column() {
- Text('right width 30%')
- .textAlign(TextAlign.Center)
- }.width('30%').backgroundColor(0xF5DEB3).height('100%')
- }.backgroundColor(0xffd306).height('30%')
- }
- }
- }
自適應(yīng)延伸是指在不同尺寸設(shè)備下,當(dāng)頁面的內(nèi)容超出屏幕大小而無法完全顯示時,可以通過滾動條進(jìn)行拖動展示。這種方法適用于線性布局中內(nèi)容無法一屏展示的場景。通常有以下兩種實(shí)現(xiàn)方式。
- @Entry
- @Component
- struct ScrollExample {
- scroller: Scroller = new Scroller();
- private arr: number[] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
- build() {
- Scroll(this.scroller) {
- Column() {
- ForEach(this.arr, (item) => {
- Text(item.toString())
- .width('90%')
- .height(150)
- .backgroundColor(0xFFFFFF)
- .borderRadius(15)
- .fontSize(16)
- .textAlign(TextAlign.Center)
- .margin({ top: 10 })
- }, item => item)
- }.width('100%')
- }
- .backgroundColor(0xDCDCDC)
- .scrollable(ScrollDirection.Vertical) // 滾動方向?yàn)榇怪狈较?/span>
- .scrollBar(BarState.On) // 滾動條常駐顯示
- .scrollBarColor(Color.Gray) // 滾動條顏色
- .scrollBarWidth(10) // 滾動條寬度
- .edgeEffect(EdgeEffect.Spring) // 滾動到邊沿后回彈
- }
- }
水平方向布局中使用Scroll組件:
- @Entry
- @Component
- struct ScrollExample {
- scroller: Scroller = new Scroller();
- private arr: number[] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
- build() {
- Scroll(this.scroller) {
- Row() {
- ForEach(this.arr, (item) => {
- Text(item.toString())
- .height('90%')
- .width(150)
- .backgroundColor(0xFFFFFF)
- .borderRadius(15)
- .fontSize(16)
- .textAlign(TextAlign.Center)
- .margin({ left: 10 })
- })
- }.height('100%')
- }
- .backgroundColor(0xDCDCDC)
- .scrollable(ScrollDirection.Horizontal) // 滾動方向?yàn)樗椒较?/span>
- .scrollBar(BarState.On) // 滾動條常駐顯示
- .scrollBarColor(Color.Gray) // 滾動條顏色
- .scrollBarWidth(10) // 滾動條寬度
- .edgeEffect(EdgeEffect.Spring) // 滾動到邊沿后回彈
- }
- }
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: