鸿蒙(HarmonyOS)—自定义TabBar适配底部安全区域+中间凸起

import { home } from './Home/home'; //我的
import { waterFlowDemo } from './propaganda'; //宣教
import {map} from "./map"
import {my} from "./MY"
import {send} from "./send"
import { statusClass } from '../../store';

@Entry
@Component
struct Index {
  @State message: string = 'Hello World';
  @State currentIndex: number = 0   //tabBar按钮点击下标
  @State BottomAvoidAreaHeight :number = 0  //获取到的系统底部安全距离
   getBottomHeightSetTimeout:number = -1  //定时器定义 用于清除
  // 控制器
  private  TabsController:TabsController =new TabsController()
   aboutToAppear(): void {
     // 获取底部安全区域 添加了定时器防止获取不到
     this.getBottomHeightSetTimeout = setTimeout(()=>{
        this.BottomAvoidAreaHeight =  JSON.parse(statusClass.getSystemStatus('BottomAvoidAreaHeight') as string)
       console.log(this.BottomAvoidAreaHeight+'底部高度'+this.getBottomHeightSetTimeout);
       if(this.BottomAvoidAreaHeight){
         clearTimeout(this.getBottomHeightSetTimeout)
       }
      },1000)
  }
  // tabBar的item
  @Builder
  tabBuilder(img:ResourceStr,selectImg:ResourceStr, text:string,index:number ){
    Column(){
      // 屏蔽中间的按钮 定位一个上去
      if(index!=2){
        Image(this.currentIndex===index?selectImg:img)
          .width(20)
          .aspectRatio(1)
        Text(text)
          .fontColor(this.currentIndex===index?Color.Green:Color.Gray)
      }
    }
  }
  build() {
    Stack({alignContent:Alignment.Bottom}){
      // tabBar设置在底部
      Tabs({barPosition:BarPosition.End, index: this.currentIndex,controller:this.TabsController}){
          TabContent(){
                home()
            .height(`calc(100% - 22vp )`) //限制组件高度 22为tabs的一半
          }.tabBar(this.tabBuilder($r('app.media.ic_public_message'),$r('app.media.ic_public_message_filled'),  '消息',0))
          .align(Alignment.Top) //内容对齐方式 必加
        TabContent(){
              map()
          }.tabBar(this.tabBuilder($r('app.media.ic_public_contacts'), $r('app.media.ic_public_contacts_filled'),'地图',1))
          TabContent(){
            send()
          }.tabBar(this.tabBuilder($r('app.media.nav_ico_fab'), $r('app.media.nav_ico_fab'),'发送',2))
          TabContent(){
            waterFlowDemo()
          }.tabBar(this.tabBuilder($r('app.media.ic_public_contacts'), $r('app.media.ic_public_contacts_filled'),'宣教',3))
          TabContent(){
            my()
          }.tabBar(this.tabBuilder($r('app.media.ic_public_contacts'), $r('app.media.ic_public_contacts_filled'),'我的',4))
        }
      .onChange((index: number) => {
        this.currentIndex = index
      })
      .barHeight(44) //默认高度
      .scrollable(false) //禁止左右滑动
      .animationDuration(0)  //点击切换动画时间
      .backgroundImage($r("app.media.tab_nav_bg")) //底部tabBar的图片
      .backgroundImageSize({width:'100%'})
      .backgroundImagePosition(Alignment.Bottom) //在Tabs的位置
      .backgroundImageResizable({slice:{bottom:this.BottomAvoidAreaHeight}}) //伸展 没起到啥作用 放着有备无患
      .backgroundColor(Color.White)
      .margin({bottom:this.BottomAvoidAreaHeight}) //将底部安全区域空出来
      // 中间的按钮
      Image($r('app.media.nav_ico_fab'))
        .width(50)
        .aspectRatio(1)
        .offset({y:-4 })
        .margin({bottom:this.BottomAvoidAreaHeight}) //将底部安全区域空出来
  }
  .height("100%")
  }
}

鸿蒙自定义底部tabbar

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值