华为鸿蒙应用--底部导航栏Tabs(自适应手机和平板)

鸿蒙ArkTS Tabs组件开发底部导航栏,可自适应平板和手机,相当于Android开发中的MainActivity+Fragment的底部导航栏模式。

一、主页:MainPage.ets
复制代码
import { BreakpointSystem, BreakpointConstants, StyleConstants, PageConstants, } from '@ohos/common';  // 通用工具
import { Chat } from '@ohos/chat';  // 子模块 相当于Android 的Fragment
import { Contact } from '@ohos/Contact';  // 子模块
import { Work } from '@ohos/Work';  //  子模块
import { Mine } from '@ohos/Mine';  //  子模块
import { buttonInfo, ButtonInfoModel } from '../viewmodel/MainPageData'; // 底部导航数据


@Entry
@Component
struct MainPage {
  @StorageProp('currentBreakpoint') currentBreakpoint: string = 'sm';
  @State currentPageIndex: number = 0;
  private breakpointSystem = new BreakpointSystem();
  aboutToAppear() {
    this.breakpointSystem.register();
  }
  aboutToDisappear() {
    this.breakpointSystem.unregister();
  }

  @Builder BottomNavigation(button: ButtonInfoModel) {
    Column({ space: PageConstants.BUTTON_SPACE }) {  //  static readonly BUTTON_SPACE: string = '6vp';
      Image(this.currentPageIndex === button.index ? button.selectImg : button.img)
        .objectFit(ImageFit.Contain)
        .width($r('app.float.main_image_size'))
        .height($r('app.float.main_image_size'))
      Text(button.title)
        .fontColor(this.currentPageIndex === button.index ? $r('app.color.focus_color') : $r('app.color.un_focus_color'))
        .opacity(this.currentPageIndex === button.index ? StyleConstants.FULL_OPACITY : StyleConstants.SIXTY_OPACITY)
        .fontWeight(StyleConstants.FONT_WEIGHT_FIVE)
        .textAlign(TextAlign.Center)
        .fontSize($r('app.float.micro_font_size'))
    }
    .width(StyleConstants.FULL_WIDTH)
    .height(StyleConstants.FULL_HEIGHT)
    .alignItems(HorizontalAlign.Center)
    .justifyContent(FlexAlign.Center)
  }

  build() {
    Column() {
      Tabs({
        barPosition: this.currentBreakpoint === BreakpointConstants.BREAKPOINT_LG ?
        BarPosition.Start : BarPosition.End,
        index: this.currentPageIndex,
      }) {
        TabContent() {
          Chat() // 子模块
        }.tabBar(this.BottomNavigation(buttonInfo[0]))

        TabContent() {
          Contact() // 子模块
        }.tabBar(this.BottomNavigation(buttonInfo[1]))

        TabContent() {
          Work() // 子模块
        }.tabBar(this.BottomNavigation(buttonInfo[2]))

        TabContent() {
          Mine() // 子模块
        }.tabBar(this.BottomNavigation(buttonInfo[3]))
      }
      .barWidth(this.currentBreakpoint === BreakpointConstants.BREAKPOINT_LG ?
      $r('app.float.bar_width') : StyleConstants.FULL_WIDTH)
      .barHeight(this.currentBreakpoint === BreakpointConstants.BREAKPOINT_LG ?
      StyleConstants.SIXTY_HEIGHT : $r('app.float.vp_fifty_six'))
      .vertical(this.currentBreakpoint === BreakpointConstants.BREAKPOINT_LG)
      .scrollable(false)
      .onChange((index: number) => {
        this.currentPageIndex = index;
        // 提前查询数据
        // if (index === 1) {
        //   this.queryShopCart();
        // } else if (index === 2) {
        //   this.queryOrderList();
        // }
      })
    }
    .backgroundColor($r('app.color.page_background'))
  }
}

StyleConstants.ets:

复制代码
/**
 * Constants for common style.
 */
export class StyleConstants {
  /**
   * Component width percentage: 100%.
   */
  static readonly FULL_WIDTH: string = '100%';

  /**
   * Component height percentage: 100%.
   */
  static readonly FULL_HEIGHT: string = '100%';

  /**
   * Component height percentage: 70%.
   */
  static readonly SEVENTY_HEIGHT: string = '70%';

  /**
   * Component height percentage: 60%.
   */
  static readonly SIXTY_HEIGHT: string = '60%';

  /**
   * Component width percentage: 60%.
   */
  static readonly SIXTY_WIDTH: string = '60%';

  /**
   * Component height percentage: 50%.
   */
  static readonly FIFTY_HEIGHT: string = '50%';

  /**
   * Component height percentage: 50%.
   */
  static readonly HUNDRED_FIFTEEN_WIDTH: string = '115%';

  /**
   * Component space vp : 4.
   */
  static readonly FOUR_SPACE: string = '4vp';

  /**
   * Component space vp : 12.
   */
  static readonly TWELVE_SPACE: string = '12vp';

  /**
   * Component space vp : 14.
   */
  static readonly ITEM_SPACE: string = '14vp';

  /**
   * Component space vp : 15.
   */
  static readonly FIFTEEN_SPACE: string = '15vp';

  /**
   * Font weight value: 700.
   */
  static readonly FONT_WEIGHT_SEVEN: number = 700;

  /**
   * Font weight value: 500.
   */
  static readonly FONT_WEIGHT_FIVE: number = 500;

  /**
   * Font weight value: 400.
   */
  static readonly FONT_WEIGHT_FOUR: number = 400;

  /**
   * Text line value: 2.
   */
  static readonly TWO_TEXT_LINE: number = 2;

  /**
   * Component opacity value: 1.
   */
  static readonly FULL_OPACITY: number = 1;

  /**
   * Component opacity value: 0.6.
   */
  static readonly SIXTY_OPACITY: number = 0.6;

  /**
   * Component opacity value: 0.8.
   */
  static readonly EIGHTY_OPACITY: number = 0.8;

  /**
   * Component layout value: 1.
   */
  static readonly LAYOUT_WEIGHT: number = 1;

  /**
   * Flex basic value: 1.
   */
  static readonly FLEX_BASIC: number = 1;

  /**
   * Flex shrink value: 1.
   */
  static readonly FLEX_SHRINK: number = 1;

  /**
   * Flex grow value: 1.
   */
  static readonly FLEX_GROW: number = 1;

  /**
   * Swiper or list display count value: 1.
   */
  static readonly DISPLAY_ONE: number = 1;

  /**
   * Swiper or list display count value: 2.
   */
  static readonly DISPLAY_TWO: number = 2;

  /**
   * Swiper or list display count value: 3.
   */
  static readonly DISPLAY_THREE: number = 3;

  /**
   * Swiper or list display count value: 4.
   */
  static readonly DISPLAY_FOUR: number = 4;

  /**
   * Image aspect ratio value: 2.23.
   */
  static readonly IMAGE_ASPECT_RATIO: number = 2.25;

  /**
   * Number of value: 0.5.
   */
  static readonly HALF_ONE: number = 0.5;

  /**
   * Number of value: -1.
   */
  static readonly MINUS_ONE: number = -1;
}
二、BreakpointSystem.ets:响应式设计的核心
复制代码
import mediaQuery from '@ohos.mediaquery';
import { BreakpointConstants } from '../constants/BreakpointConstants';

declare interface BreakPointTypeOption<T> {
  sm?: T
  md?: T
  lg?: T
  xl?: T
  xxl?: T
}

/**
 * 媒体查询(mediaquery)
 * 响应式设计的核心
 */
export class BreakPointType<T> {
  options: BreakPointTypeOption<T>

  constructor(option: BreakPointTypeOption<T>) {
    this.options = option
  }

  getValue(currentBreakPoint: string): T {
    return this.options[currentBreakPoint] as T
  }
}

export class BreakpointSystem {
  private currentBreakpoint: string = '';
  private smListener?: mediaQuery.MediaQueryListener;
  private mdListener?: mediaQuery.MediaQueryListener;
  private lgListener?: mediaQuery.MediaQueryListener;

  private updateCurrentBreakpoint(breakpoint: string) {
    if (this.currentBreakpoint !== breakpoint) {
      this.currentBreakpoint = breakpoint;
      AppStorage.Set<string>(BreakpointConstants.CURRENT_BREAKPOINT, this.currentBreakpoint);
    }
  }

  private isBreakpointSM = (mediaQueryResult: mediaQuery.MediaQueryResult) => {
    if (mediaQueryResult.matches) {
      this.updateCurrentBreakpoint(BreakpointConstants.BREAKPOINT_SM);
    }
  }
  private isBreakpointMD = (mediaQueryResult: mediaQuery.MediaQueryResult) => {
    if (mediaQueryResult.matches) {
      this.updateCurrentBreakpoint(BreakpointConstants.BREAKPOINT_MD);
    }
  }
  private isBreakpointLG = (mediaQueryResult: mediaQuery.MediaQueryResult) => {
    if (mediaQueryResult.matches) {
      this.updateCurrentBreakpoint(BreakpointConstants.BREAKPOINT_LG);
    }
  }

  public register() {
    this.smListener = mediaQuery.matchMediaSync(BreakpointConstants.RANGE_SM);
    this.smListener.on('change', this.isBreakpointSM);
    this.mdListener = mediaQuery.matchMediaSync(BreakpointConstants.RANGE_MD);
    this.mdListener.on('change', this.isBreakpointMD);
    this.lgListener = mediaQuery.matchMediaSync(BreakpointConstants.RANGE_LG);
    this.lgListener.on('change', this.isBreakpointLG);
  }

  public unregister() {
    this.smListener?.off('change', this.isBreakpointSM);
    this.mdListener?.off('change', this.isBreakpointMD);
    this.lgListener?.off('change', this.isBreakpointLG);
  }
}
复制代码
BreakpointConstants.ets: 
复制代码
export class BreakpointConstants {
  /**
   * Breakpoints that represent small device types.
   */
  static readonly BREAKPOINT_SM: string = 'sm';

  /**
   * Breakpoints that represent middle device types.
   */
  static readonly BREAKPOINT_MD: string = 'md';

  /**
   * Breakpoints that represent large device types.
   */
  static readonly BREAKPOINT_LG: string = 'lg';

  /**
   * Current breakpoints that to query the device types.
   */
  static readonly CURRENT_BREAKPOINT: string = 'currentBreakpoint';

  /**
   * Range of the small device width.
   */
  static readonly RANGE_SM: string = '(320vp<=width<520vp)';

  /**
   * Range of the middle device width.
   */
  static readonly RANGE_MD: string = '(520vp<=width<840vp)';

  /**
   * Range of the large device width.
   */
  static readonly RANGE_LG: string = '(840vp<=width)';
}
三、ButtonInfoModel.ets:底部导航按钮数据
复制代码
export class ButtonInfoModel {
  index: number ;  // 序号
  img: Resource;  // 未选中图片
  selectImg: Resource ; // 选中图片
  title: Resource; // 按钮名称
}

const buttonInfo: ButtonInfoModel[] = [
  {
    index: 0,
    img: $r('app.media.ic_chat_off'),
    selectImg: $r('app.media.ic_chat_on'),
    title: $r('app.string.chat')
  },
  {
    index: 1,
    img: $r('app.media.ic_contact_off'),
    selectImg: $r('app.media.ic_contact_on'),
    title: $r('app.string.contact')
  },
  {
    index: 2,
    img: $r('app.media.ic_work_off'),
    selectImg: $r('app.media.ic_work_on'),
    title: $r('app.string.work')
  },
  {
    index: 3,
    img: $r('app.media.ic_mine_off'),
    selectImg: $r('app.media.ic_mine_on'),
    title: $r('app.string.mine')
  }
]

export { buttonInfo }
相关推荐
火狮2 小时前
鸿蒙Next API 12开发,使用@ohos/axios进行HTTP请求
http·华为·harmonyos
SuperHeroWu74 小时前
【HarmonyOS】HarmonyOS和React Native混合开发 (一)之环境安装
react native·harmonyos·鸿蒙·开发环境·环境安装·rn·混合开发
轻口味4 小时前
【每日学点鸿蒙知识】AVCodec、SmartPerf工具、web组件加载、监听键盘的显示隐藏、Asset Store Kit
前端·华为·harmonyos
无处安放的波澜4 小时前
【HarmonyOS 5.0】第十二篇-ArkUI公共属性(一)
华为·harmonyos·arkts·鸿蒙·鸿蒙系统
李洋-蛟龙腾飞公司5 小时前
HarmonyOS Next 应用元服务开发-分布式数据对象迁移数据文件资产迁移
分布式·华为·harmonyos
大土豆的bug记录6 小时前
鸿蒙历史搜索功能:tag标签根据文字宽度自动换行 展示更多
华为·harmonyos
轻口味6 小时前
【每日学点鸿蒙知识】Charles抓包、lock文件处理、WebView组件、NFC相关、CallMethod失败等
华为·harmonyos
凯子坚持 c6 小时前
编程新选择:深入了解仓颉语言的优雅与高效
华为
一个处女座的程序猿O(∩_∩)O7 小时前
开源鸿蒙 5.0 正式版发布
华为·harmonyos
程序猿会指北8 小时前
【鸿蒙(HarmonyOS)性能优化指南】内存分析器Allocation Profiler
性能优化·移动开发·harmonyos·openharmony·arkui·组件化·鸿蒙开发