HarmonyOS ArkTS Tab使用方法(十五)

一,使用方法

Android中Java代码使用fragment进行Tab切换,下面使用HarmonyOS ArkTS 语言实现Tab的使用,代码如下:

/*
 * Copyright (c) 2022 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import CommonConstants from '../common/constants/CommonConstants';
import Home from "../view/Home"
import Setting from "../view/Setting"

/**
 * Main page
 */
@Entry
@Component
struct MainPage {
   
  @State currentIndex: number = CommonConstants.HOME_TAB_INDEX;
  private tabsController: TabsController = new TabsController();

  @Builder TabBuilder(title: string, index: number, selectedImg: Resource, normalImg: Resource) {
   
    Column() {
   
      Image(this.currentIndex === index ? selectedImg : normalImg)
        .width($r('app.float.mainPage_baseTab_size'))
        .height($r('app.float.mainPage_baseTab_size'))
      Text(title)
        .margin({
    top: $r('app.float.mainPage_baseTab_top') })
        .fontSize($r('app.float.main_tab_fontSize'))
        .fontColor(this.currentIndex === index ? $r('app.color.mainPage_selected') : $r('app.color.mainPage_normal'))
    }
    .justifyContent(FlexAlign.Center)
    .height($r('app.float.mainPage_barHeight'))
    .width(CommonConstants.FULL_PARENT)
    .onClick(() => {
   
      this.currentIndex = index;
      this.tabsController.changeIndex(this.currentIndex);
    })
  }

  build() {
   
    Tabs({
   
      barPosition: BarPosition.End,
      controller: this.tabsController
    }) {
   
      TabContent() {
   
           //需要切换的tab,自己定义
      }
      .padding({
    left: $r('app.float.mainPage_padding'), right: $r('app.float.mainPage_padding') })
      .backgroundColor($r('app.color.mainPage_backgroundColor'))
      .tabBar(this.TabBuilder(CommonConstants.HOME_TITLE, CommonConstants.HOME_TAB_INDEX,
        $r('app.media.home_selected'), $r('app.media.home_normal')))

      TabContent() {
   
          //需要切换的tab,自己定义
      }
      .padding({
    left: $r('app.float.mainPage_padding'), right: $r('app.float.mainPage_padding') })
      .backgroundColor($r('app.color.mainPage_backgroundColor'))
      .tabBar(this.TabBuilder(CommonConstants.MINE_TITLE, CommonConstants.MINE_TAB_INDEX,
        $r('app.media.mine_selected'), $r('app.media.mine_normal')))
    }
    .width(CommonConstants.FULL_PARENT)
    .backgroundColor(Color.White)
    .barHeight($r('app.float.mainPage_barHeight'))
    .barMode(BarMode.Fixed)
    .onChange((index: number) => {
   
      this.currentIndex = index;
    })
  }
}

二、效果图如下

在这里插入图片描述

相关推荐

  1. 高级优化理论与方法

    2023-12-22 01:42:01       22 阅读
  2. 中介子方程

    2023-12-22 01:42:01       27 阅读
  3. 中介子方程

    2023-12-22 01:42:01       28 阅读
  4. 中介子方程

    2023-12-22 01:42:01       29 阅读
  5. 设计模式详解()——模板方法模式

    2023-12-22 01:42:01       42 阅读
  6. C语言第二弹--模拟使用strncat

    2023-12-22 01:42:01       49 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-22 01:42:01       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-22 01:42:01       101 阅读
  3. 在Django里面运行非项目文件

    2023-12-22 01:42:01       82 阅读
  4. Python语言-面向对象

    2023-12-22 01:42:01       91 阅读

热门阅读

  1. 蓝桥杯-每日刷题-023

    2023-12-22 01:42:01       51 阅读
  2. Linux命令行控制小米电源开关

    2023-12-22 01:42:01       48 阅读
  3. Solidity-5-表达式和控制结构

    2023-12-22 01:42:01       41 阅读
  4. PyQt中的冒号(:)

    2023-12-22 01:42:01       54 阅读
  5. RHCE8 资料整理(十二)

    2023-12-22 01:42:01       62 阅读
  6. Issues about Ubuntu & ROS

    2023-12-22 01:42:01       49 阅读
  7. 【版本管理】git stash用法

    2023-12-22 01:42:01       51 阅读
  8. vue跳转方式

    2023-12-22 01:42:01       62 阅读
  9. HarmonyOS开发(十二):ArkTS组件通用事件

    2023-12-22 01:42:01       48 阅读