仓颉应用开发之——组件底部导航(21)

一 概述

  • 底部导航实现介绍
  • 底部导航常用类及方法
  • 导航示例

二 底部导航实现介绍

  • 仓颉底部导航使用:Tabs(页签)+TabContent(内容视图)
  • Tabs:对应底部导航栏内容,通过TabBar设置,有两个属性:icon-图标,text-文本
  • TabContent:tabBar对应的显示内容,导入一个@Component自定义视图

三 底部导航常用类及方法

No 类或方法 说明
1 Tabs(BarPosition.Start, this.controller) 页签
2 TabContent {} 页签对应显示
3 func tabBar(icon!: CJResource, text!: CJResource) 页签Item

四 导航示例

4.1 主界面-index.cj

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package ohos_app_cangjie_entry

import ohos.base.*
import ohos.component.*
import ohos.state_manage.*
import ohos.state_macro_manage.*
import ohos_app_cangjie_entry.pages.*
import ohos_app_cangjie_entry.bean.*
import ohos_app_cangjie_entry.api.*
import ohos.router.*
import std.collection.*
import encoding.json.*
import serialization.serialization.*
import ohos.hilog.*
import ohos.resource_manager.*

@Entry
@Component
class MyView {
@State var message: String = "Hello Cangjie"
var controller: TabsController = TabsController()
@State var currentIndex: Int32 = 0

protected override func onPageShow() {

}
func build() {
Column() {
Tabs(BarPosition.End, this.controller) {
TabContent {
TabContentView(currentIndex:this.currentIndex)
}
.tabBar(icon: getResourceMedia(@r(app.media.startIcon)), text: "Tab1")
.id("tab0")
TabContent {
TabContentView(currentIndex:this.currentIndex)
}
.tabBar(icon: getResourceMedia(@r(app.media.startIcon)), text: "Tab2")
.id("tab1")
TabContent() {
TabContentView(currentIndex:this.currentIndex)
}
.tabBar(icon: getResourceMedia(@r(app.media.startIcon)), text: "Tab3")
.id("tab2")
TabContent() {
TabContentView(currentIndex:this.currentIndex)
}
.tabBar(icon: getResourceMedia(@r(app.media.startIcon)), text: "Tab4")
.id("tab3")
}
.vertical(false)
.scrollable(true)
.barMode(BarMode.Fixed)
.barWidth(100.percent)
.barHeight(70)
.animationDuration(400)
.width(100.percent)
.backgroundColor(0xF5F5F5)
.onChange({index: Int32 => this.currentIndex = index})
}
.width(100.percent)
.height(100.percent)
.margin(top: 5)
}
}

4.2 TabContent内容TabContentView

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package ohos_app_cangjie_entry.pages
internal import ohos.base.*
internal import ohos.component.*
internal import ohos.state_manage.*
import ohos.state_macro_manage.*

@Component
public class TabContentView {
@Link var currentIndex: Int32

func build() {
Column() {
Text("Tab${this.currentIndex + 1}")
.textAlign(TextAlign.Center)
.fontSize(50)
.fontColor(0x182431)
.opacity(0.4)
.height(100.percent)
}
.width(100.percent)
.height(100.percent)
}
}

4.3 效果图

五 参考

  • HarmonyOS NEXT Beta1-TabContent
  • HarmonyOS NEXT Beta1-Tabs