鸿蒙期末-黑马健康2

首页Tabs


思维导图:


该页面整体为一个Tabs组件,在Tabs组件中包含了TabBar和TabContent

代码实现:

import BreakpointType from '../common/bean/BreanpointType'
import BreakpointConstants from '../common/constants/BreakpointConstants'
import { CommonConstants } from '../common/constants/CommonConstants'
import BreakpointSystem from '../common/utils/BreakpointSystem'
import RIndex from '../view/record/RIndex'
@Entry
@Component
struct Index2{
  @State currentIndex :number= 0//下面的排列默认为序号0即记录列面
  private breakpointSystem:BreakpointSystem=new BreakpointSystem()
  @StorageProp('currentBreakpoint')currentBreakpoint:string = BreakpointConstants.BREAKPOINT_SM

  @State isPageShow:boolean =false

  onPageShow(){
    this.isPageShow=true
  }
  onPageHide(){
    this.isPageShow=false
  }

  @Builder TabBarBuilder(title:Resource,image:ResourceStr,index:number){
  Column({space:CommonConstants.SPACE_8}){
    Image(image)
      .width(22)
      .fillColor(this.selectColor(index))//fillColor图片要为svg格式才可
    Text(title)
      .fontSize(14)
      .fontColor(this.selectColor(index))
  }

 }
  aboutToAppear(){
    this.breakpointSystem.register()
  }
  aboutToDisappear(){
    this.breakpointSystem.unregister()
  }

  selectColor(index:number){//角标与当前角标做判断,颜色变化
      return this.currentIndex===index ? $r('app.color.primary_color') : $r('app.color.gray')
  }

  
  build(){
    Tabs({barPosition:BreakpointConstants.BAR_POSITION.getValue(this.currentBreakpoint)}){
      TabContent(){//没有这个子组件的话Tabs下只能用文字
        //RIndex({isPageShow:this.isPageShow})
        Text('记录页面')
      }
      .tabBar(this.TabBarBuilder($r('app.string.tab_record'),$r('app.media.ic_calendar'),0))

      TabContent(){
        Text('发现页面')
      }

      .tabBar(this.TabBarBuilder($r('app.string.tab_discover'),$r('app.media.discover'),1))
      TabContent(){
        Text('我的主页')
      }
      .tabBar($r('app.string.tab_user'))
      .tabBar(this.TabBarBuilder($r('app.string.tab_user'),$r('app.media.ic_user_portrait'),2))
    }
    .width('100%')
    .height('100%')
    .onChange(index=>this.currentIndex=index)//记录切换的角标
    .vertical(new BreakpointType({//布局模式
      sm:false,
      md:true,
      lg:true
    }).getValue(this.currentBreakpoint))
  }

}


运行截图

 


总结


        上述代码主要存在index页面,用于显示首页面。在此页面中,通过Tabs组件下的TabContent与TabBar组件来布局,TabBarBuilder来定义TabBar中的图片文字和角标
定义一个selectColor组件来实现角标与当前角标做判断实现颜色变化
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值