实战案例-食物列表页(源于黑马程序员)

分析食物列表页面,可以发现其是占整个页面的列布局(column容器),内部由顶部列表项(一个行布局(row容器):返回按钮和页面名称),下边是可以切换页面的tabs组件(切换选项卡组件)。

ItemIndex页面:

import router from '@ohos.router'
import { CommonConstants } from '../common/constants/CommonConstants'
import ItemList from '../view/Item/ItemList'
@Entry
@Component
struct ItemIndex {
  @State message: string = 'Hello World'

  build() {
    Column() {
      //1.头部导航
      this.Header()
      //2.列表
      ItemList()
    }
    .height('100%')
    .height('100%')
  }

  @Builder Header(){//顶部导航
    Row(){//行布局
      Image($r('app.media.ic_public_back'))
        .width(24)
        .onClick(()=>router.back())//点击后返回上个页面
      Blank()
      Text('早餐')//显示点击的记录分组名称
        .fontSize(18)
        .fontWeight(CommonConstants.FONT_WEIGHT_600)
    }
    .width(CommonConstants.THOUSANDTH_940)
    .height(32)
  }

}

 ItemList页面:

import { CommonConstants } from '../../common/constants/CommonConstants'
@Component
export default struct ItemList {//食物列表
  build() {
    Tabs(){//切换组件卡组件
      TabContent(){
        this.TabContentBuilder()
      }
      .tabBar('全部')

      TabContent(){
        this.TabContentBuilder()
      }
      .tabBar('主食')

      TabContent(){
        this.TabContentBuilder()
      }
      .tabBar('肉蛋奶')
    }
    .width(CommonConstants.THOUSANDTH_940)
    .height('100%')
  }

  @Builder TabContentBuilder(){//将List组件封装到TabContentBuilder组件中
    List({space:CommonConstants.SPACE_10}){
      ForEach([1,2,3,4,5,6],(item)=>{
        ListItem(){
          Row({space:CommonConstants.SPACE_6}){
            Image($r('app.media.toast'))
              .width(50)
            Column({space:CommonConstants.SPACE_4}){//列表中一项数据有两行(列布局)
              Text('全麦吐司')
                .fontWeight(CommonConstants.FONT_WEIGHT_500)
              Text('91千卡/片')
                .fontSize(14)
                .fontColor($r('app.color.light_gray'))
            }
            Blank()
            Image($r('app.media.ic_public_add_norm_filled'))
              .width(18)
              .fillColor($r('app.color.primary_color'))//填充颜色
          }
          .width('100%')
          .padding(CommonConstants.SPACE_6)
        }
      })
    }
    .width('100%')
    .height('100%')
  }
}

运行结果:(三个页面可以互相切换)

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值