HarmonyOS 鸿蒙Next开发,底部Tab样式如何做成弧形的样式?
HarmonyOS 鸿蒙Next开发,底部Tab样式如何做成弧形的样式? 鸿蒙开发,底部Tab样式如何做成弧形的样式?有开发过的兄弟帮忙解答一下,谢谢!
4 回复
可以通过自定义tabBar实现,可以参考下以下示例:
const ARC_MARGIN_TOP: number = -30; // 圆弧的上外间距为-30
export class TabBarDataType {
id: number;
defaultIcon: ResourceStr;
constructor(id: number, defaultIcon: ResourceStr) {
this.id = id;
this.defaultIcon = defaultIcon;
}
}
export const TABINFO: TabBarDataType[] = [
new TabBarDataType(0, $r('app.media.startIcon')),
new TabBarDataType(1, $r('app.media.startIcon')),
new TabBarDataType(2, $r('app.media.startIcon')),
new TabBarDataType(3, $r('app.media.startIcon')),
new TabBarDataType(4, $r('app.media.startIcon')),
];
@Entry
@Component
struct Index {
@Provide selectedIndex: number = 0; // 初始化被选定的tabBar下标
private controller: TabsController = new TabsController(); // 初始化Tab控制器
build() {
Column() {
Tabs({ index: this.selectedIndex, barPosition: BarPosition.End, controller: this.controller }) {
TabContent() {
Text("tab1")
.fontSize(26)
}
TabContent() {
Text("tab2")
.fontSize(26)
}
TabContent() {
Text("tab3")
.fontSize(26)
}
TabContent() {
Text("tab4")
.fontSize(26)
}
TabContent() {
Text("tab5")
.fontSize(26)
}
}
.vertical(false)
.scrollable(false)
.layoutWeight(1)
.backgroundColor('#ffdbd9d9')
.barHeight(0)
.onChange((index: number) => {
this.selectedIndex = index;
})
// 自定义TabBar组件
CustomTabBar({ selectedIndex: $selectedIndex })
}.width("100%")
.height("100%")
}
}
@Component
struct CustomTabBar {
@Link selectedIndex: number; // 初始化被选定的tabBar下标
build() {
Flex({ direction: FlexDirection.Row, justifyContent: FlexAlign.SpaceAround, alignItems: ItemAlign.Center }) {
ForEach(TABINFO, (item: TabBarDataType, tabIndex: number) => {
// 单独一个tabBar组件
TabItem({
tabBarIndex: tabIndex,
selectedIndex: $selectedIndex,
})
})
}
.height(60)
}
}
@Component
struct TabItem {
@Prop tabBarIndex: number; // tabBar下标
@Link selectedIndex: number; // 初始化被选定的tabBar下标
build() {
Column() {
if (this.tabBarIndex === this.selectedIndex) {
Column() {
Image(TABINFO[this.tabBarIndex].defaultIcon)
.size({ width: 43, height: 43 })
.interpolation(ImageInterpolation.High)
.borderRadius(22)
}
.width(52)
.height(52)
.borderRadius(24)
.margin({ top: ARC_MARGIN_TOP })
.backgroundColor(Color.White)
.justifyContent(FlexAlign.Center)
} else {
Column() {
// 通过被选中的tabBar下标值和tabBar的默认下标值来改变图片显示
Image(TABINFO[this.tabBarIndex].defaultIcon)
.interpolation(ImageInterpolation.High)
.size(
{ width: 28, height: 28 })
.borderRadius(14)
}
.width(37)
.height(37)
.justifyContent(FlexAlign.Center)
}
}
.width(60)
.onClick(() => {
// 更新被选中的tabBar下标
this.selectedIndex = this.tabBarIndex;
})
}
}
官方也有自定义tabBar的示例demo可以参考下:自定义TabBar页签凸起和凹陷案例
更多关于HarmonyOS 鸿蒙Next开发,底部Tab样式如何做成弧形的样式?的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
【背景知识】
Tabs:通过页签进行内容视图切换的容器组件,每个页签对应一个内容视图。
【参考方案】:
可参考基于Tabs组件实现常见导航样式示例,通过基于Tabs组件,为开发者提供不同场景下的导航样式。
- 常规型底部导航用Tab组件实现。
- 双层嵌套样式:第一层用Tab组件,第二层用List实现嵌套。
- 可滑动+更多左侧用List实现,右侧为更多图标,下滑线设置margin值并在onAreaChange方法中改变margin值可实现移动, scrollToIndex实现切换时Tab栏显示并居中。
- 背景高亮样式和常见侧边导航用List实现。
- 抽屉式导航用SideBarContainer实现。
- TabContent视频内容滑动:创建Tabs组件,将barHeight设置为0;创建自定义TabBar;将TabContent的内容分为上下两部分,上半部存放video组件,下部分高度存放进度条,设置滑动手势响应事件;将Tabs组件的zIndex 属性设置为2,TabContent的视图就可以堆叠在自定义TabBar之上;再设置hitTestBehavior属性使被覆盖的自定义的TabBar可以响应点击事件。
HarmonyOS Next中实现弧形底部Tab可通过以下方式:使用ShapeContainer组件定义弧形路径,设置arcTo方法绘制曲线。在Tab组件外层嵌套ShapeContainer,通过Edge.Arc参数调整弧度。使用Column布局结合position定位确保Tab内容与弧形边缘适配。具体弧度数值需通过Radius参数调节,建议使用百分比布局确保不同屏幕尺寸下的显示一致性。
在HarmonyOS Next中,可以通过自定义布局和绘制实现底部Tab的弧形样式。以下是关键步骤:
- 使用
ShapeElement
或自定义Component
绘制弧形背景,通过Path
定义曲线路径,并设置填充颜色。 - 将Tab栏容器(如
Row
)的背景设置为该自定义组件,调整高度和弧度参数。 - 结合
Tabs
和TabContent
组件布局内容区域,确保内容不会被弧形区域遮挡。
示例代码片段(ArkTS):
// 自定义弧形背景组件
@Component
struct ArcBackground {
build() {
Path()
.commands('M0 0 L100 0 L100 80 C50 100 50 100 0 80 Z') // 调整路径控制点形成弧度
.fill(Color.White)
}
}
// Tab栏布局
@Entry
@Component
struct ArcTabExample {
build() {
Column() {
// 内容区域
TabContent() { /* ... */ }
// 底部Tab栏
Row() {
// Tab项...
}
.height(80)
.background(new ArcBackground()) // 应用弧形背景
}
}
}
通过调整Path
中的控制点坐标可精确控制弧度形态,需注意设备屏幕宽度适配。