鸿蒙开发深入浅出01(基本环境搭建、页面模板与TabBar)

鸿蒙开发深入浅出01(基本环境搭建、页面模板与TabBar)

1、效果展示

2、下载 DevEco Studio

3、创建项目



4、新建页面模板


java 复制代码
/**
* ${PROJECT_NAME} #项目的名称 
* ${FILE_NAME} #文件名称
* Created by ${USER} ON ${DATE} #作者及添加日期
*/
@Entry
@Component
struct ${NAME} {
  build() {
    
  }
}

5、更改应用信息

json 复制代码
{
  "string": [
    {
      "name": "module_desc",
      "value": "module description"
    },
    {
      "name": "EntryAbility_desc",
      "value": "description"
    },
    {
      "name": "EntryAbility_label",
      "value": "硅谷租房" # 应用名称
    }
  ]
}

6、新建以下页面

  • Home.ets
  • See.ets
  • Discover.ets
  • Service.ets
  • My.ets

7、Index.ets

js 复制代码
import Home from './Home'
import See from './See'
import Service from './Service'
import Discover from './Discover'
import My from './My'

@Entry
@Component
struct Index {
  @State currentTabBarIndex: number = 0;

  @Builder
  tabBarBuilder(image: Resource, activeImage: Resource, text: string, index: number) {
    Column() {
      Image(this.currentTabBarIndex == index ? activeImage : image).width(28).height(28)
      Text(text).fontSize(10).fontColor(this.currentTabBarIndex == index ? '#000' : '#CBCBCB')
    }
  }

  build() {
    Tabs({ barPosition: BarPosition.End }) {
      TabContent() {
        Home()
      }.tabBar(this.tabBarBuilder($r('app.media.tabbar_home'), $r('app.media.tabbar_home_active'), '首页', 0))

      TabContent() {
        See()
      }.tabBar(this.tabBarBuilder($r('app.media.tabbar_see'), $r('app.media.tabbar_see_active'), '想看', 1))

      TabContent() {
        Service()
      }.tabBar(this.tabBarBuilder($r('app.media.tabbar_service'), $r('app.media.tabbar_service_active'), '服务', 2))

      TabContent() {
        Discover()
      }.tabBar(this.tabBarBuilder($r('app.media.tabbar_discover'), $r('app.media.tabbar_discover_active'), '发现', 3))

      TabContent() {
        My()
      }.tabBar(this.tabBarBuilder($r('app.media.tabbar_my'), $r('app.media.tabbar_my_active'), '我的', 4))
    }.barHeight(50)
    .scrollable(false)
    .onChange((index) => {
      this.currentTabBarIndex = index;
    })
  }
}

8、真机运行



9、图片资源文件

见资源绑定

相关推荐
xq95274 小时前
鸿蒙next 获取versionCode和versionName
harmonyos
鸿蒙小白龙4 小时前
openharmony之恢复出厂设置需求总结
harmonyos·鸿蒙·鸿蒙系统
深海的鲸同学 luvi5 小时前
【HarmonyOS】H5 实现在浏览器中正常跳转 AppLinking 至应用
华为·harmonyos
挨踢攻城6 小时前
华为 | SD-WAN场景丢包类问题定位
网络·华为·php·hcie·hcia·hcip·厦门微思网络
zhanshuo17 小时前
HarmonyOS 实战:学会在鸿蒙中使用第三方 JavaScript 库(附完整 Demo)
harmonyos
zhanshuo17 小时前
鸿蒙应用权限处理全攻略:从配置到相机拍照,一篇文章讲透
harmonyos
AlbertZein19 小时前
HarmonyOS5 凭什么学鸿蒙—— GetContext
架构·harmonyos
森之鸟1 天前
flutter项目适配鸿蒙
flutter·华为·harmonyos
奶糖不太甜1 天前
鸿蒙图片资源加载全攻略:从基础到性能优化
harmonyos·图片资源
小小小小小星1 天前
鸿蒙多端适配开发指南:从入门到实战
harmonyos