当前位置:网站首页>[harmonyos] [arkui] Hongmeng ETS method tabs+tabcontent realizes the bottom navigation bar
[harmonyos] [arkui] Hongmeng ETS method tabs+tabcontent realizes the bottom navigation bar
2022-07-18 17:00:00 【Huawei Developer Forum】
In Hongmeng development tab Switch function ( As shown in the figure below ) Is a very common function , Today, describe how to realize the following functions ? What materials need to be prepared in development ?
Today we have “ Data preparation ”,“Tabs Function realization ”,“ Bottom button function realization ”,“ Running effect ” Four aspects are described



1. The development of preparation
1.1 Data preparation If you want to realize the function shown in the figure above , Need to learn “Tabs”,“TabContent”,“ Row”,“Column”, And so on
1.2 Picture preparation Prepare six pictures ( The picture is as follows ) Put it in resources/base/media/ Under the table of contents

Where pictures are stored

2. Tabs Function realization
2.1 Details reference “Tabs”,“TabContent” Official documents of
The code is as follows
@Entry
@Component
struct MyNewsIndex {
private controller: TabsController = new TabsController()
build() {
Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
Tabs({ controller: this.controller }) {
TabContent() {
Text(" home page ")
.width('100%').height('100%')
.fontSize(50)
.textAlign(TextAlign.Center)
.fontColor(Color.White)
.backgroundColor(Color.Pink)
}.tabBar(' home page ')
TabContent() {
Text(" news ")
.width('100%').height('100%')
.fontSize(50)
.textAlign(TextAlign.Center)
.fontColor(Color.White)
.backgroundColor(Color.Blue)
}.tabBar(' news ')
TabContent() {
Text(" my ")
.width('100%').height('100%')
.fontSize(50)
.textAlign(TextAlign.Center)
.fontColor(Color.White)
.backgroundColor(Color.Red)
}.tabBar(' my ')
}
.scrollable(false)
.barHeight(0)
.animationDuration(0)
}.alignItems(VerticalAlign.Bottom).width('100%').height(120).margin({top:0,right:0,bottom:10,left:0})
}
.width('100%')
.height('100%')
}
}
The effect is as follows

3 Bottom button function realization
3.1 The bottom function realization mainly uses “Row”,“Column”,“Text”,“Image” And other related technologies , The code is as follows
Row() {
Column(){
Image($r('app.media.index_select'))
.width(60).height(60)
Text(' home page ')
.size({ width: '100%', height: 60 }).textAlign(TextAlign.Center)
.fontSize(20)
.fontColor(Color.Red)
}
.layoutWeight(1)
.backgroundColor(0xFFEFD5)
.height("100%")
Column(){
Image($r('app.media.msg_unselect'))
.width(60).height(60)
Text(' news ')
.size({ width: '100%', height: 60 }).textAlign(TextAlign.Center)
.fontSize(20)
.fontColor(Color.Black)
}
.layoutWeight(1)
.backgroundColor(0xFFEFD5)
.height("100%")
Column(){
Image($r('app.media.my_unselect'))
.width(60).height(60)
Text(' my ')
.size({ width: '100%', height: 60 }).textAlign(TextAlign.Center)
.fontSize(20)
.fontColor(Color.Black)
}
.layoutWeight(1)
.backgroundColor(0xFFEFD5)
.height("100%")
}.alignItems(VerticalAlign.Bottom).width('100%').height(120).margin({top:0,right:0,bottom:10,left:0})4 Running effect
4.1 Tabs And button linkage
We are defining a global variable SelectPos For the currently selected index , When the button is clicked, the current index is assigned , Also on Image And font color , All codes are as follows
@Entry
@Component
struct MyNewsIndex {
private controller: TabsController = new TabsController()
@State SelectPos:number=0;
public IndexClick(){
this.SelectPos=0;
this.controller.changeIndex(0)
}
public messageClick(){
this.SelectPos=1;
this.controller.changeIndex(1)
}
public myClick(){
this.SelectPos=2;
this.controller.changeIndex(2)
}
build() {
Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
Tabs({ controller: this.controller }) {
TabContent() {
Text(" home page ")
.width('100%').height('100%')
.fontSize(50)
.textAlign(TextAlign.Center)
.fontColor(Color.White)
.backgroundColor(Color.Pink)
}.tabBar(' home page ')
TabContent() {
Text(" news ")
.width('100%').height('100%')
.fontSize(50)
.textAlign(TextAlign.Center)
.fontColor(Color.White)
.backgroundColor(Color.Blue)
}.tabBar(' news ')
TabContent() {
Text(" my ")
.width('100%').height('100%')
.fontSize(50)
.textAlign(TextAlign.Center)
.fontColor(Color.White)
.backgroundColor(Color.Red)
}.tabBar(' my ')
}
.scrollable(false)
.barHeight(0)
.animationDuration(0)
Row() {
Column(){
Image((this.SelectPos==0?$r('app.media.index_select'):$r('app.media.index_unselect')))
.width(60).height(60)
Text(' home page ')
.size({ width: '100%', height: 60 }).textAlign(TextAlign.Center)
.fontSize(20)
.fontColor((this.SelectPos==0?Color.Red:Color.Black))
}
.layoutWeight(1)
.backgroundColor(0xFFEFD5)
.height("100%")
.onClick(this.IndexClick.bind(this))
Column(){
Image((this.SelectPos==1?$r('app.media.msg_select'):$r('app.media.msg_unselect')))
.width(60).height(60)
Text(' news ')
.size({ width: '100%', height: 60 }).textAlign(TextAlign.Center)
.fontSize(20)
.fontColor((this.SelectPos==1?Color.Red:Color.Black))
}
.layoutWeight(1)
.backgroundColor(0xFFEFD5)
.height("100%")
.onClick(this.messageClick.bind(this))
Column(){
Image((this.SelectPos==2?$r('app.media.my_select'):$r('app.media.my_unselect')))
.width(60).height(60)
Text(' my ')
.size({ width: '100%', height: 60 }).textAlign(TextAlign.Center)
.fontSize(20)
.fontColor((this.SelectPos==2?Color.Red:Color.Black))
}
.layoutWeight(1)
.backgroundColor(0xFFEFD5)
.height("100%")
.onClick(this.myClick.bind(this))
}.alignItems(VerticalAlign.Bottom).width('100%').height(120).margin({top:0,right:0,bottom:10,left:0})
}
.width('100%')
.height('100%')
}
}The operation effect is as follows


More relevant learning materials :
https://developer.huawei.com/consumer/cn/forum/topic/0201787474595650145?fid=0102683795438680754?ha_source=zzh
边栏推荐
- 【golang学习笔记】接口(interface)
- js自写一个适用于object对象的forEach方法
- 【HarmonyOS】【ARKUI】鸿蒙 ets方式tabs+tabcontent 实现底部导航栏
- 【Golang】切片(slice)
- 从IT视角审视企业经营,B2B行业CIO谈如何从“成本中心”转到“增长中心”?
- US pressure surges tiktok changes global safety director
- module ‘urllib‘ has no attribute ‘urlretrieve‘
- 无996无内卷,乐视过上了“神仙日子”?
- One stop Devops platform makes a big difference in development
- 够速度,才激情!2022 Amazon DeepRacer 7月赛高调来袭
猜你喜欢

Error yolov5 PT to onnx error

藏红花是否会成为用于关节炎患者的安全和有效的自然疗法?

从IT视角审视企业经营,B2B行业CIO谈如何从“成本中心”转到“增长中心”?

在创建生成WIFI二维码手机扫码链接

vscode 1.69变化与关注点(三路合并/终端集成等)
![Aardio - [library] graphical paging component library](/img/c3/8f0c77c50f551b20439364a3eca236.png)
Aardio - [library] graphical paging component library

深度学习(DL, Deep Learning)入门1——LetNet5网络结构

Enough speed, passion! The July 2022 Amazon deeperacer competition is high-profile

【手把手带你学UVM】~ 记录遇到的一切错误

1.4 流程控制语句
随机推荐
從 URL 輸入到頁面展現到底發生什麼?
OSPF(Open Shortest Path First開放式最短路徑優先)防環以及計算錯誤帶來的組網應用隱患
US pressure surges tiktok changes global safety director
How JMeter uses MD5 encryption and fingerprint signature on the body
View for recording a temperature curve
Directory structure analysis of unity Foundation
在创建生成WIFI二维码手机扫码链接
Error yolov5 PT to onnx error
重装mysql后initializing database错误
【AGC】增长服务3-App Linking示例
在线SQL转YAML工具
【Harmony OS】【FAQ】鸿蒙应用开发问题分享(字体/构造器)
Web APIs DOM event flow
藏红花是否会成为用于关节炎患者的安全和有效的自然疗法?
【快应用】text组件里的文字很多,旁边的div样式会被拉伸如何解决
With "technology as the boat", rpa+ai injects surging momentum into the new retail digital transformation
[quick application] quick application user agreement and privacy policy content can jump many times. Click back and fail to return to the previous page. How to deal with it?
【机器学习】 逻辑回归原理及代码
Why PCB test is needed!
Health prevention guide 2: healthy diet