当前位置:   article > 正文

鸿蒙Arkts开发框架经验分享之主页面Tabs_鸿蒙arkts tab

鸿蒙arkts tab

目录

Tabs语法

1.1 HomePage()

1.2 .tabBar参数输入


Tabs语法

  1. @Entry
  2. @Component
  3. struct community{
  4. @StorageProp('currentDeviceSize') currentDeviceSize:string=CommonConstants.SM;
  5. @State currentIndex:number=CommonConstants.HOME_TAB_INDEX;
  6. aboutToAppear(){
  7. MultipleDevicesUtils.unregister();
  8. }
  9. build()
  10. {
  11. Tabs({
  12. barPosition: this.currentDeviceSize === CommonConstants.LG ?
  13. BarPosition.Start : BarPosition.End,
  14. index: this.currentIndex
  15. })
  16. {
  17. TabContent() {
  18. HomePage()
  19. }
  20. .tabBar(this.TabBuilder(CommonConstants.HOME_TITLE, CommonConstants.HOME_TAB_INDEX,
  21. $r('app.media.ic_home_selected'), $r('app.media.ic_home_normal')))
  22. TabContent(){
  23. Challenges()
  24. }
  25. .tabBar(this.TabBuilder(CommonConstants.CHALLENGE_TITLE,CommonConstants.HOME_TAB_INDEX,
  26. $r('app.media.ic_challen_selected'),$r('app.media.ic_challen_normal')))
  27. TabContent() {
  28. MinePage()
  29. }
  30. .tabBar(this.TabBuilder(CommonConstants.MINE_TITLE, CommonConstants.MINE_TAB_INDEX,
  31. $r('app.media.ic_mine_selected'), $r('app.media.ic_mine_normal')))
  32. }
  33. .barWidth(this.currentDeviceSize === CommonConstants.LG ?
  34. StyleConstants.BAR_WIDTH_LG : StyleConstants.FULL_PARENT)
  35. .barHeight(this.currentDeviceSize === CommonConstants.LG ?
  36. StyleConstants.BAR_HEIGHT_LG : StyleConstants.BAR_DEFAULT_HEIGHT)
  37. .vertical(this.currentDeviceSize === CommonConstants.LG)
  38. .backgroundColor(Color.White)
  39. .barMode(BarMode.Fixed)
  40. .scrollable(false)
  41. .onChange((index: number) => {
  42. this.currentIndex = index;
  43. Logger.info(CommonConstants.TAG_MAIN_PAGE, 'onChange index ' + JSON.stringify(index));
  44. })
  45. }
  46. @Builder TabBuilder(title: string, index: number, selectedImg: Resource, normalImg: Resource) {
  47. Column() {
  48. Image(this.currentIndex === index ? selectedImg : normalImg)//通过当前活跃的currentIndex和页签对应的targetIndex匹配与否
  49. .width($r('app.float.base_tab_size')) //从而决定UI显示的样式
  50. .height($r('app.float.base_tab_size'))
  51. Text(title)
  52. .margin({ top: $r('app.float.base_tab_top') })
  53. .fontSize($r("app.float.tab_font_size"))
  54. .fontColor(this.currentIndex === index ? $r('app.color.main_page_selected') : $r('app.color.main_page_normal'))
  55. }
  56. .justifyContent(FlexAlign.Center)
  57. .height(StyleConstants.FULL_PARENT)
  58. .width(StyleConstants.FULL_PARENT)
  59. }
  60. }

在这一段代码中,需要注意:

一是以函数HomePage()为TabContent的内容,以便开发者更方便的debug和开发

二是在属性.tabBar输入参数的过程中,使用了TabBuilder这个自定义函数进行,最终效果为点击便可以改变图片

1.1 HomePage()

代码段:

(输入主页的页面代码)

对于这个函数的引用:

需要使用import语法进行插入:

import HomePage from '../view/HomePage';

对于这个函数的定义:

1)文件放在/ets/view/当中

2)函数定义前加 export default struct

在其中,Text的输入参数是存放在/src/main/resources/base/element/string.json中,方便改代码进行整理,在这个文件中存放了很多字符串常量,这个优势在于可以使用resources下的文件zh_CN和en_US进行语言切换管理

在接下来内容中,SwiperView()等函数进行了引用

SwiperView()函数定义

在其中的核心函数是:

//Swiper(){

}中的函数//

ForEach函数:

接口描述:

ForEach(

arr: Array,//数据源,可以设置为返回值为数组类型的函数,必选项

itemGenerator: (item: any, index: number) => void, //组件生成函数,item参数:arr数组中的数据项,必选项

keyGenerator?: (item: any, index: number) => string //键值生成函数,item参数:arr数组中的数据项

)

getSwiperImages()函数

  1. @Preview //预览装饰器
  2. @Component
  3. struct SwiperView {//轮播图函数
  4. @StorageProp('currentDeviceSize') currentDeviceSize: string = CommonConstants.SM;
  5. build() {
  6. Column() {
  7. Swiper() {
  8. ForEach(mainViewModel.getSwiperImages(), (item: Resource) => {
  9. //mainViewModel这个包里面有getSwiperImages函数
  10. //且arr这个传参的实参为getSwiperImages
  11. //箭头函数:item是形参,Resource是形参的数据类型
  12. Image(item)
  13. .borderRadius($r('app.float.swiper_radius'))
  14. }, (item: Resource) => JSON.stringify(item))
  15. }
  16. .displayCount(CommonConstants.SWIPER_CACHE_COUNT)
  17. .autoPlay(true)//该轮播图是自动播放
  18. .width(StyleConstants.COMMON_WIDTH)
  19. .itemSpace(this.currentDeviceSize === CommonConstants.SM ?
  20. 0 : StyleConstants.ITEM_SPACE)
  21. .margin($r('app.float.common_margin'))
  22. .displayCount(this.currentDeviceSize === CommonConstants.SM ?
  23. StyleConstants.SWIPER_COUNT_ONE :
  24. (this.currentDeviceSize === CommonConstants.MD ?
  25. StyleConstants.SWIPER_COUNT_TWO : StyleConstants.SWIPER_COUNT_THREE))
  26. }
  27. }
  28. }

1.2 .tabBar参数输入

这个参数输入是一个本ets界面定义的函数,这个函数是以@Builder为装饰器

@Builder装饰器:

装饰的函数遵循build()函数语法规则,在@Builder定义的函数中,分为:全局自定义构建函数,使用的时候用function进行修饰;局部自定义构建函数,不用function进行修饰。

TabBuilder函数主体:

  1. @Builder TabBuilder(title: string, index: number, selectedImg: Resource, normalImg: Resource) {
  2. Column() {
  3. Image(this.currentIndex === index ? selectedImg : normalImg)
  4. //通过当前活跃的currentIndex和页签对应的targetIndex匹配与否
  5. //从而决定UI显示的样式
  6. .width($r('app.float.base_tab_size'))
  7. .height($r('app.float.base_tab_size'))
  8. Text(title)
  9. .margin({ top: $r('app.float.base_tab_top') })
  10. .fontSize($r("app.float.tab_font_size"))
  11. .fontColor(this.currentIndex === index ? $r('app.color.main_page_selected') : $r('app.color.main_page_normal'))
  12. }
  13. .justifyContent(FlexAlign.Center)
  14. .height(StyleConstants.FULL_PARENT)
  15. .width(StyleConstants.FULL_PARENT)
  16. }

其中currentIndex在build()函数中定义为@state变量

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小舞很执着/article/detail/757332
推荐阅读
相关标签
  

闽ICP备14008679号