HarmonyOS鸿蒙Next中为什么使用Tabs组件的tabbar,在子元素有五个时会出现一条分隔线?
HarmonyOS鸿蒙Next中为什么使用Tabs组件的tabbar,在子元素有五个时会出现一条分隔线?
代码如下:
主文件:
import { Route, ZRouter } from "@hzw/zrouter"
import { AppUtil, ToastUtil } from "@pura/harmony-utils"
import { HomeView } from "home"
import { MineView } from "mine"
import { QuestionView } from "question"
import { SchemeView } from "scheme"
import { TabModel } from "uicomponents"
import { CommonConst, NavName } from "utils"
import { tabBarModel } from "../model/MainModel"
/**
* Author:J
* Describe: 主页
*/
// 定义一个状态变量来记录上次点击返回键的时间
let lastBackPressedTime = 0
@Preview
@ComponentV2
@Route({ name: NavName.MAIN_PAGE })
export struct MainPage {
@Local selectedIndex: number = 0 // 当前选中的tab下标
@Local msgCount: number = 9 // 消息数量
//存储页面状态
@Local tabContentArr: boolean[] = [true, false, false, false]
aboutToAppear(): void {
console.log(`xxx : ---` + 'MainPage')
console.log(`xxx获取参数 : ---` + ZRouter.getInstance().getParamByName(NavName.MAIN_PAGE))
}
build() {
NavDestination() {
Stack() {
Tabs({ index: this.selectedIndex, barPosition: BarPosition.End }) {
ForEach(tabBarModel, (item: TabModel, index: number) => {
TabContent() {
if (this.selectedIndex === index || this.tabContentArr[index]) {
this.tabContentBuilder(item)
}
}.tabBar(this.tabBottom(tabBarModel[item.index], item.index))
}, (item: string) => item)
}.barWidth(CommonConst.FULL_PARENT)
.barHeight(56) //设置导航栏高度
.scrollable(false) // 禁止左右滑动
.onChange((index: number) => {
this.selectedIndex = index;
this.tabContentArr[index] = true;
})
}.width(CommonConst.FULL_PARENT)
.height(CommonConst.FULL_PARENT)
}.hideTitleBar(true)
.width(CommonConst.FULL_PARENT)
.height(CommonConst.FULL_PARENT)
.onBackPressed(() => {
const currentTime = new Date().getTime()
const timeDifference = currentTime - lastBackPressedTime
if (timeDifference < 2000) { // 2秒内再次点击
//退出应用
AppUtil.exit()
} else {
// 提示用户
ToastUtil.showToast('再按一次退出应用')
lastBackPressedTime = currentTime
}
return true
})
}
@Builder
tabContentBuilder(item: TabModel) {
if (item.index == 0) {
// 首页
HomeView()
} else if (item.index == 1) {
// 问答
QuestionView()
} else if (item.index == 2) {
// 体系
SchemeView()
} else if (item.index == 3) {
// 我的
MineView()
}
}
@Builder
tabBottom(item: TabModel, index: number) {
Column() {
Badge({
count: index != 1 ? 0 : this.msgCount,
position: BadgePosition.RightTop,
style: {
fontSize: 8,
badgeSize: 13
}
}) {
Image(item.selectImage /*this.selectedIndex == index ? item.selectImage : item.unSelectImage*/)
.colorBlend(this.selectedIndex == index ? $r('app.color.colorPrimary') : $r('app.color.color_222222'))
.height(24)
.margin(4)
}.margin({ top: 4 })
Text(item.title)
.width(CommonConst.FULL_PARENT)
.fontSize(14)
.fontWeight(500)
.textAlign(TextAlign.Center)
.fontColor(this.selectedIndex == index ? $r('app.color.colorPrimary') : $r('app.color.color_222222'))
.margin({ bottom: 4 })
}
.width(CommonConst.FULL_PARENT)
.height(CommonConst.FULL_PARENT)
.backgroundColor('#cc1C1C1D')
}
}
model文件:
import { TabModel } from "uicomponents";
/**
* 首页底部Tab显示数据
*/
export const tabBarModel: Array<TabModel> = [
{
index: 0,
title: '首页',
selectImage: $r('app.media.ic_bottom_bar_home'),
},
{
index: 1,
title: '问答',
selectImage: $r('app.media.ic_bottom_bar_ques'),
},
{
index: 2,
title: '体系',
selectImage: $r("app.media.ic_bottom_bar_scheme"),
},
{
index: 3,
title: '我的',
selectImage: $r('app.media.ic_bottom_bar_mine'),
}
,
// {
// index: 4,
// selectImage:$r('app.media.ictab'),
// title: '我的',
//
// }
]
更多关于HarmonyOS鸿蒙Next中为什么使用Tabs组件的tabbar,在子元素有五个时会出现一条分隔线?的实战教程也可以访问 https://www.itying.com/category-93-b0.html
2 回复
在HarmonyOS鸿蒙Next中,Tabs组件的tabbar出现分隔线是系统默认设计行为。当tabbar子元素数量超过4个时,系统会自动添加分隔线以提升视觉区分度。该设计遵循鸿蒙UX设计规范,目的是防止过多tab项造成拥挤感。分隔线样式由系统主题控制,开发者无法直接通过组件属性关闭,但可以通过自定义tabbar样式覆盖默认实现。
更多关于HarmonyOS鸿蒙Next中为什么使用Tabs组件的tabbar,在子元素有五个时会出现一条分隔线?的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
这是HarmonyOS Next中Tabs组件的默认设计行为。当tabbar子元素数量达到5个时,系统会自动添加一条分隔线来提升视觉区分度。这是系统内置的样式规则,主要出于以下考虑:
- 交互设计规范:5个及以上tab项时,分隔线可以帮助用户更清晰地区分各个tab
- 视觉平衡:防止过多tab项造成视觉拥挤
- 一致性:保持与其他系统应用的设计语言统一
解决方法:
-
如果不需要分隔线,可以通过自定义样式覆盖:
Tabs() { // tab内容 }.barStyle({ divider: null })
-
或者调整bar的背景色来隐藏分隔线:
.backgroundColor('#cc1C1C1D') // 使用与分隔线相同的颜色