Implementing a full bottom tab functionality in HarmonyOS NEXT using ArkTS can be done in the following steps:
-
Creating Tabs Components: Use
Tabs
component to create the bottom navigation bar with thebarPosition
property sets its position to the bottom ()。
-
Add TabContent subcomponent: in
Tabs
component internally, creating aTabContent
subcomponents that will contain the content of each tab page. -
Configuring the TabBar: By
TabContent
(used form a nominal expression)tabBar
property to configure the title and icon of each tab. Custom functions can be usedtabBuilder
to build the style of each tab, including icons and text. -
Setting Status and Controller: Use
@State
decorator to define the index of the currently selected tab and use theTabsController
to control switching between tabs. -
Custom Styles: This can be done by
barMode
、scrollable
and other properties to customize the style and behavior of the navigation bar, such as setting it to a fixed width or scrolling display. -
event processing: By
onChange
event to listen for tab switching and update the current state.
Below is a simple code example showing how to implement the bottom tab functionality using ArkTS:
@Entry
@Component
struct BottomTabExample {
controller: TabsController = new TabsController()
@State current: number = 0
tabBuilder($$: { index: number; label: string; normalIcon: Resource; selectIcon: Resource }) {
Column() {
Image( === $$.index ? $$.selectIcon : $$.normalIcon).width(26)
Text($$.label)
.fontSize('12fp')
.fontColor( === $$.index ? '#62C9D0' : '#909090')
.margin({ top: 3 })
}
.width('100%')
.onClick(() => {
= $$.index
()
})
}
build() {
Column() {
Tabs({ barPosition: , controller: }) {
TabContent() {
Text('Content of the home page')
}.tabBar(({
index: 0,
label: 'home page (of a website)',
normalIcon: $r('.tabbar11'),
selectIcon: $r('.tabbar12')
}))
TabContent() {
Text('What was found')
}.tabBar(({
index: 1,
label: 'discoveries',
normalIcon: $r('.tabbar21'),
selectIcon: $r('.tabbar22')
}))
// ... (sth. or sb) elseTabContentconfigure
}
.width('100%')
.barMode()
.scrollable(true)
.onChange(((index: number) => {
= index
}))
}
.width('100%')
.backgroundColor('#f2f2f2')
}
}
We created a bottom tab, each with corresponding content and icons. When the user clicks on a different tab, thetabBuilder
function updates the index of the currently selected tab, and theTabsController
will handle the page switch. This is accomplished through theonChange
events, we can listen to the changes in the tab and execute the corresponding logic. Learning Hongmeng NEXT development, domestic application development, all rely on you and me, cheers.