HarmonyOS鸿蒙Next中tab栏如何点击空白地方可以隐藏tab栏

HarmonyOS鸿蒙Next中tab栏如何点击空白地方可以隐藏tab栏 这种的点击空白区域隐藏tab栏式如何实现的,有简单的demo案例吗


更多关于HarmonyOS鸿蒙Next中tab栏如何点击空白地方可以隐藏tab栏的实战教程也可以访问 https://www.itying.com/category-93-b0.html

12 回复
@Entry
@Component
struct Index {
  @State currentIndex: number = 0;
  @State isShow: boolean = true;

  @Builder
  TabContentBuilder(content: string, index: number) {
    Column() {
      Text(content)
        .fontSize(24)
        .fontColor('#333')
    }
    .width('100%')
    .height('100%')
    .justifyContent(FlexAlign.Center)
  }

  build() {
    Stack({ alignContent: Alignment.Bottom }) {
      Tabs({ index: this.currentIndex, barPosition: BarPosition.End }) {
        TabContent() {
          this.TabContentBuilder('内容 1', 0)
        }

        TabContent() {
          this.TabContentBuilder('内容 2', 1)
        }

      }
      .width('100%')
      .height('100%')
      .barHeight(0)
      .onChange((index: number) => {
        this.currentIndex = index;
      })
      .expandSafeArea([SafeAreaType.SYSTEM], [SafeAreaEdge.BOTTOM, SafeAreaEdge.TOP])


      // 控制按钮 - 左上角
      Button(this.isShow ? '隐藏 Tabs' : '显示 Tabs')
        .position({ x: 20, y: 60 })
        .onClick(() => {
          this.isShow = !this.isShow;
        })

      // 自定义 TabBar - 抽屉动画
      Row() {
        Text('页签1')
          .fontSize(16)
          .fontWeight(FontWeight.Medium)
          .fontColor(this.currentIndex === 0 ? '#007DFF' : '#999999')
          .layoutWeight(1)
          .textAlign(TextAlign.Center)
          .onClick(() => {
            this.currentIndex = 0
          })

        Text('页签2')
          .fontSize(16)
          .fontWeight(FontWeight.Medium)
          .fontColor(this.currentIndex === 1 ? '#007DFF' : '#999999')
          .layoutWeight(1)
          .textAlign(TextAlign.Center)
          .onClick(() => {
            this.currentIndex = 1
          })

      }
      .expandSafeArea([SafeAreaType.SYSTEM], [SafeAreaEdge.BOTTOM,])

      .width('100%')
      .height(56)
      .backgroundColor('#FFFFFF')
      .border({ width: { top: 1 }, color: '#E5E5E5' })
      // 抽屉动画
      .translate({ y: this.isShow ? 0 : 56 })
      .opacity(this.isShow ? 1 : 0)
      .animation({
        duration: 300,
        curve: Curve.EaseInOut
      })
    }
    .width('100%')
    .height('100%')
  }
}

更多关于HarmonyOS鸿蒙Next中tab栏如何点击空白地方可以隐藏tab栏的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


给空白区域设置点击事件就好了呀,

重点不是点击而是怎么实现隐藏tabs栏

有要学HarmonyOS AI的同学吗,联系我:https://www.itying.com/goods-1206.html

设置偏移量,看看有没有参数叫offset或translate,我不是专业做鸿蒙开发的,不知道在鸿蒙里对应的参数叫什么。

看图中的效果应该是做了y方向的位移。

针对您提到的Tab的问题,您可以参考HdsTab

如果要使用动画,可以参考animateto配合组件的translate可以实现

我写了一个小demo可以实现您想要的效果,可以做参考

代码如下:

import { HdsTabs } from "@hms.hds.hdsBaseComponent"


@Entry
export struct TestHdsTab {

  @State isShow:boolean=true

  @State translateY:number=0



  build() {


    Column() {
      Row({space:20}) {
        SymbolGlyph($r("sys.symbol.arrow_left")).onClick(() => {
          this.getUIContext().getRouter().back()
        })



        Button("隐藏").onClick(()=>{
          this.getUIContext().animateTo({
            duration:1000,
            curve:Curve.EaseOut,
            iterations:1,
            playMode:PlayMode.Normal
          },()=>{
            this.translateY=50
          })

          setTimeout(()=>{
            this.isShow=false
          },1000)
        })

        Button("显示").onClick(()=>{
          this.translateY=0
          this.isShow=true
        })

      }.width('100%').justifyContent(FlexAlign.Start).height(40)

      Row() {
        HdsTabs(){
          TabContent() {
          }
          .tabBar({ icon: $r('app.media.startIcon'), text: '页签1' })

          TabContent() {
          }
          .tabBar({ icon: $r('app.media.startIcon'), text: '页签2' })
        }
        .translate({ y: this.translateY })
        .vertical(false)
        .barPosition(BarPosition.End)
        .visibility(this.isShow?Visibility.Visible:Visibility.None)
      }.layoutWeight(1)

    }
  }
}

在tab上添加visibility的属性,空白位置设置点击事件

再空白区域添加一个点击事件,然后tab上添加visibility的属性,如果tab是显示 的状态,设置为不可显示

.visibility(this.isShow?Visibility.Visible:Visibility.None)

在HarmonyOS Next中,可通过在TabContent组件上绑定触摸事件实现点击空白处隐藏TabBar。使用onTouch事件监听,当检测到点击区域不在TabBar范围内时,调用TabBuilder的showTabBar(false)方法隐藏。需结合HitTestMode和区域计算判断点击位置。

在HarmonyOS Next中,可以通过监听页面的触摸事件,判断点击位置是否在TabBar区域外,从而实现点击空白处隐藏TabBar。核心是使用@ohos.router模块进行页面路由,并结合@ohos.window模块获取窗口信息进行坐标判断。

以下是关键步骤和代码示例:

1. 页面布局与状态管理

在页面组件中,使用@State装饰器控制TabBar的显示/隐藏状态。

@Entry
@Component
struct Index {
  @State isTabBarVisible: boolean = true // 控制TabBar显示状态

  // 页面主要内容
  build() {
    Column() {
      // 你的页面内容...
      Text('点击空白区域试试')
        .onClick(() => {
          // 点击内容区域可触发隐藏
          this.isTabBarVisible = false
        })
    }
  }
}

2. 获取TabBar组件引用

为TabBar组件添加id,以便在事件处理中获取其区域信息。

@Component
struct MyTabBar {
  build() {
    Row() {
      // Tab项...
    }
    .id('tabBar') // 为TabBar设置id
  }
}

3. 监听全局触摸事件

在页面中覆盖一个透明的触摸层,用于捕获空白区域的点击:

Column() {
  // 页面内容...
}
.width('100%')
.height('100%')
.onTouch((event: TouchEvent) => {
  if (event.type === TouchType.Down) {
    this.handleBlankAreaTouch(event)
  }
})

4. 判断点击区域

在触摸事件处理函数中,判断点击位置是否在TabBar区域外:

import { getInspectorByKey } from '@ohos.arkui.node'

handleBlankAreaTouch(event: TouchEvent) {
  const tabBarNode = getInspectorByKey('tabBar') // 通过id获取TabBar节点
  if (!tabBarNode) return
  
  const tabBarRect = tabBarNode.getRect()
  const touchX = event.touches[0].x
  const touchY = event.touches[0].y
  
  // 判断点击是否在TabBar区域外
  const isOutsideTabBar = touchX < tabBarRect.left || 
                        touchX > tabBarRect.right || 
                        touchY < tabBarRect.top || 
                        touchY > tabBarRect.bottom
  
  if (isOutsideTabBar) {
    this.isTabBarVisible = false
  }
}

5. 完整示例结构

@Entry
@Component
struct MainPage {
  @State isTabBarVisible: boolean = true

  build() {
    Stack({ alignContent: Alignment.Bottom }) {
      // 主内容区域
      Column() {
        Text('页面内容区域')
          .fontSize(20)
          .margin(20)
      }
      .width('100%')
      .height('100%')
      .onTouch((event) => {
        if (event.type === TouchType.Down) {
          this.handleTouch(event)
        }
      })

      // 条件渲染TabBar
      if (this.isTabBarVisible) {
        MyTabBar()
      }
    }
  }

  handleTouch(event: TouchEvent) {
    // 区域判断逻辑...
  }
}

注意事项:

  • 这种方法需要在全页面层拦截触摸事件,注意避免影响页面内其他组件的正常交互
  • 如果页面内容可滚动,需要处理好触摸事件冲突
  • 显示/隐藏动画可以通过animateTo添加过渡效果

实际开发中,建议将TabBar封装为独立组件,通过@Provide@Consume装饰器实现跨组件状态同步,使代码更易维护。

回到顶部