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

这篇具有很好参考价值的文章主要介绍了华为鸿蒙应用--底部导航栏Tabs(自适应手机和平板)-ArkTs。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

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

华为鸿蒙应用 csdn 底部导航栏,HarmonyOS,harmonyos,华为,鸿蒙系统华为鸿蒙应用 csdn 底部导航栏,HarmonyOS,harmonyos,华为,鸿蒙系统

一、主页: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:文章来源地址https://www.toymoban.com/news/detail-810931.html

/**
 * 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 }

到了这里,关于华为鸿蒙应用--底部导航栏Tabs(自适应手机和平板)-ArkTs的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 鸿蒙原生应用开发-折叠屏、平板设备服务卡片适配

    为不同尺寸的卡片提供不同的功能 在卡片开发过程中请考虑适配不同尺寸的设备,特别是在折叠屏和平板设备上,设备屏幕尺寸的变化直接影响了卡片内容的展示。请发挥想象力设计具有自适应能力的卡片,避免在卡片内容不做任何处理的情况下直接适配成较大尺寸,原则上

    2024年02月03日
    浏览(35)
  • appium+华为鸿蒙手机自动化(环境配置)

    安装python appium client,cmd中输入 pip install appium-python-client 等待安装完成即可。 JDK官方网址:Download 建议选择exe,可以选择路径,而且安装完成之后,java的环境变量都会保存在javapath的目录中。 如果你的JDK版本不是新的,那么你在安装完成之后,可能需要配置一下环境变量。

    2023年04月26日
    浏览(40)
  • 华为手机(鸿蒙OS)开启adb调试权限

    背景:做自动化测试的应该就不用我解释adb是什么了吧,现在需要开启华为手机的adb调试权限,别上来就搜开发者选项,肯定是搜不到的,按照如下配置逐步来就行了,找不到就直接进入设置最上面有个搜索框 1、进入设置-关于手机-版本号(连续点击7次) 2、这里面有两次认

    2024年02月11日
    浏览(33)
  • 【鸿蒙应用ArkTS开发系列】- 导航栏Tab组件使用讲解

    现在市场上的大部分应用,主页都是才用底部导航栏菜单作为页面主体框架来展示, 在鸿蒙中是使用Tabs组件实现,下面我们开始讲解Tab组件的使用。 Tabs是一个通过页签进行内容视图切换的容器组件,每个页签对应一个内容视图,它仅可包含子组件TabContent,同时搭配 TabsCo

    2024年01月16日
    浏览(88)
  • 二手荣耀华为手机/平板以前用的账号密码都忘了如何清除解开.激活重置恢复找回解开方法教程

    二手荣耀华为手机/平板以前用的账号密码都忘了,现在打开设备锁定华为手机号码变更如何解锁激活码荣耀华为手机/平板激活需要输入账号密码 不记得了怎么进去号也停了我给强制恢复出厂设置了,结果手机被锁定了,现在激活设备激活不了,需要账号的密的我的华为荣耀

    2024年02月10日
    浏览(160)
  • 华为新发布的鸿蒙与目前华为手机的鸿蒙系统有什么差异,OpenHarmony、HarmonyOS、HarmonyOS NEXT的区别详解

    最近工作中需要进行鸿蒙适配,一开始我有个疑问,今年新发布的鸿蒙系统,与目前华为手机使用的鸿蒙系统有什么差异?为什么要专门进行适配?如果大家也有类似的疑问,看完这篇就明白了。 今年华为在鸿蒙生态千帆启航仪式上正式发布了鸿蒙原生操作系统——HarmonyO

    2024年04月17日
    浏览(42)
  • 鸿蒙手机华为meta 30 第一次链接电脑下载驱动

    第一步下载 华为手机助手HiSuite官方下载 | 华为官网 usb连接电脑后要手机要开启 USB调试模式,具体开启方式点击手机设置-》关于手机-》快速点击五次HarmonyOs版本 然后就进入了开发者模式 ,点击返回-》系统和更新-》开发人员选项-》开启并开启usb调试  第二部设置    

    2024年02月16日
    浏览(37)
  • 华为手机Mate9不能升级鸿蒙系统的解决方法

    升级方式 进入手机设置-系统和更新-软件更新(或者进入系统应用【服务】内更新),进入后会自动检测;检测到版本,直接下载安装,下载安装后,查看版本是否是鸿蒙,如果不是,继续检测,直到提示已经是最新版本或未检测到有新的安装包之类的提示。 遇到的问题 我

    2024年02月12日
    浏览(51)
  • adb wifi连接调试应用--适用于手机、平板、电视TV等

    今天需要adb WiFi连接电视调试东西,发现使用了网上的方法后还是无法连接adb WiFi成功,后来摸索了下,便成功了,根本原因还是需要确保电脑、手机或者电视他们在同一个WiFi内。我的 做法就是电脑开个热点,手机或者电视连接这个热点就肯定没有问题了  首先电脑开个热点

    2024年02月10日
    浏览(35)
  • 华为鸿蒙开发者工具DevEco Studio设备栏不识别鸿蒙手机模拟器解决方法

    在学习鸿蒙开发的路上,有很多小伙伴遇到过安装了手机模拟器,但是开发工具设备栏不识别手机设备的问题,如下图,明明模拟器都安装了,但为什么设备栏不显示呢? 已windows系统为例,解决方法如下 当开启DevEco Studio工具和鸿蒙手机模拟器后,手机设备不识别。需要先关

    2024年02月02日
    浏览(146)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包