美文网首页
构建更加丰富的页面=>案例解析1

构建更加丰富的页面=>案例解析1

作者: it奔跑在路上 | 来源:发表于2023-12-11 13:23 被阅读0次
官网案例
gitee源码地址

相关概念

  build() {
    Column() {
      //标题
      this.titleBar()
      //内容区域
      TargetInformation(
           ...
      )
      //子目标
      TargetList(
            ...
      )
  }

一、标题

main
├── ets
│   ├── entryability
│   │   └── EntryAbility.ts
│   └── pages   //核心代码的位置
│       └── Index.ets 
├── resources
│   ├── base
│   │   ├── element
│   │   │   ├── color.json
│   │   │   └── string.json
│   │   ├── media
│   │   │   └── icon.png
│   │   └── profile
│   │       └── main_pages.json
│   ├── en_US
│   │   └── element
│   │       └── string.json
│   ├── rawfile
│   └── zh_CN
│       └── element
│           └── string.json
└── module.json5
index.ets代码
@Entry
@Component
struct Index {

  build() {
    Column() {
      this.titleBar()
    }
    .width('100%')
    .height('100%')
    .backgroundColor('#f1f3f5')
  }

  @Builder titleBar(){
    Text('工作目标')
      .fontSize(24)
      .fontWeight(FontWeight.Bold)
      .textAlign(TextAlign.Start)
      .width('86.7%')
      .margin({top:20,bottom:10})

  }
}

首先,实现了标题的功能


image.png

二、内容区域

新建文件夹view,新建TargetInformation类


image.png

在index.ets中导入TargetInformation

    Column() {
      //标题
      this.titleBar()
      //内容区域
      TargetInformation()
    }

在TargetInformation 先定义边框背景

@Component
export default struct TargetInformation {
  build() {
    Column() {
      //TODO
      this.TargetItem()
    }
    .padding(16)
    .width('93.3%')
    .height(187)
    .backgroundColor(Color.White)
    .borderRadius(24)
  }
}

至此,我们完成了一个空白的背景页面


image.png

=> 开始填充背景页面

@Component
export default struct TargetInformation {
  build() {
    Column() {
      this.TargetItem()
    }
    .padding(16)
    .width('93.3%')
    .height(187)
    .backgroundColor(Color.White)
    .borderRadius(24)
  }

  @Builder TargetItem(){
    Row(){
      Image($r('app.media.ic_main'))
        .width(96)//宽度
        .height(96)//高度
        .objectFit(ImageFit.Fill)//图片填充方式
        .borderRadius(12)//图片圆角

      Column(){
        Text('第一季度运营目标')
          .fontSize(18)
          .fontWeight(FontWeight.Bold)
          .width('86.7%')
        Text('第一季度运营目标')
          .opacityTextStyle()
          .fontWeight(15)
          .margin({top:12})
      }
      .margin({left:'5%'})
      .alignItems(HorizontalAlign.Start)
    }
  }
}

@Extend(Text) function opacityTextStyle() {
  .fontColor('#182431')
  .fontSize(12)
  .opacity(0.4)
  .fontWeight(FontWeight.Medium)

}
image.png

tips:
图片控件是用 $r 的方式,图片资源在app.media文件夹下

=> 继续完善内容区域

@Component
export default struct TargetInformation {

  build() {
    Column() {
      this.TargetItem()
      this.OverallProgress()
    }
    .padding(16)
    .width('93.3%')
    .height(187)
    .backgroundColor(Color.White)
    .borderRadius(24)
  }

  @Builder TargetItem(){
    Row(){
      Image($r('app.media.ic_main'))
        .width(96)//宽度
        .height(96)//高度
        .objectFit(ImageFit.Fill)//图片填充方式
        .borderRadius(12)//图片圆角

      Column(){
        Text('第一季度运营目标')
          .fontSize(18)
          .fontWeight(FontWeight.Bold)
          .width('86.7%')
        Text('实现用户量与用户活跃度提升')
          .opacityTextStyle()
          .fontSize(15)
          .margin({top:12})
      }
      .margin({left:'5%'})
      .alignItems(HorizontalAlign.Start)
    }
  }

  @Builder OverallProgress(){
    Row(){
      Column(){
        Text('整体进度')
          .fontColor('#182431')
          .fontSize(16)
          .fontWeight(FontWeight.Bold)
        Row(){
          Text('更新时间:')
            .opacityTextStyle()
          Text('2023-12-13 22:42')
            .opacityTextStyle()
        }
      }
      .alignItems(HorizontalAlign.Start)

      Stack(){
        Row(){
          Text('1')
            .fontSize(14)
            .fontColor('#007dff')
            .fontWeight(FontWeight.Bold)
          Text('/3')
            .fontSize(14)
            .fontWeight(FontWeight.Bold)
        }

        Progress({
          value:1,
          total:3,
          type:ProgressType.Ring
        })
          .color('#007dff')
          .style({
            strokeWidth:4.8
          })
          .width(48)
          .height(48)
      }
    }
    .justifyContent(FlexAlign.SpaceBetween)
    .width('100%')
    .height(48)
    .margin({top:15})
  }
}

@Extend(Text) function opacityTextStyle() {
  .fontColor('#182431')
  .fontSize(12)
  .opacity(0.4)
  .fontWeight(FontWeight.Medium)
}
image.png

至此,我们完成了内容区域,但是会发现数据都是写死的,我们从index.ets传过来。

修改index.ets中的代码

struct Index {
  @State latestUpdateDate: string = '2023-12-13 23:00'
  @State totalTasksNumber: number = 3
  @State completedTasksNumber: number = 1

  build() {
    Column() {
      //标题
     ...
      //内容区域
      TargetInformation({
        latestUpdateDate: this.latestUpdateDate,
        totalTasksNumber: this.totalTasksNumber,
        completedTasksNumber: this.completedTasksNumber
      })
    }
    ...
  }
  ...
}

修改TargetInformation.ets中的代码

@Component
export default struct TargetInformation {

  @Prop latestUpdateDate:string
  @Prop totalTasksNumber:number
  @Prop completedTasksNumber:number

  ...
  @Builder OverallProgress(){
    Row(){
      Column(){
        ...
        Row(){
          Text('更新时间:')
            .opacityTextStyle()
          Text(this.latestUpdateDate)
            .opacityTextStyle()
        }
      }
      .alignItems(HorizontalAlign.Start)

      Stack(){
        Row(){
          Text(this.completedTasksNumber.toString())
            .fontSize(14)
            .fontColor('#007dff')
            .fontWeight(FontWeight.Bold)
          Text(`/${this.totalTasksNumber}`)
            .fontSize(14)
            .fontWeight(FontWeight.Bold)
        }

        Progress({
          value:this.completedTasksNumber,
          total:this.totalTasksNumber,
          type:ProgressType.Ring
        })
          .color('#007dff')
          .style({
            strokeWidth:4.8
          })
          .width(48)
          .height(48)
      }
    }
    .justifyContent(FlexAlign.SpaceBetween)
    .width('100%')
    .height(48)
    .margin({top:15})
  }
}
...
image.png

至此,内容区域大功告成,我们只需要从index.ets中修改latestUpdateDate、totalTasksNumber、completedTasksNumber即可。

相关文章

  • 90-10 好图~一图胜千言

    今天来个嘎嘣脆,直接上案例哦。 案例一: 案例解析: 1、信息才是页面最重要的因素 ...

  • 浏览器渲染页面过程

    浏览器渲染页面过程 1. 构建DOM和CSSOM树 浏览器渲染页面前需要构建DOM和CSSOM树 浏览器解析过程大...

  • 使用Java开发HarmonyOS

    开发准备 本文适用于HarmonyOS应用开发的初学者。通过构建一个简单的具有页面跳转功能的应用(如下图预览器运行...

  • 页面构建

    页面构建阶段从浏览器接收页面代码开始。其执行分为两个阶段: html解析和dom构建 javascript等脚本代...

  • 页面渲染及性能优化

    页面渲染的过程 构建DOM树解析HTML,创建DOM树,解析过程:如果遇到link & style,就会去下载这些...

  • 页面渲染过程与回流重绘

    页面渲染过程 解析HTML,生成DOM树,解析CSS,生成CSSOM树 结合(DOM)和(CSSOM)构建一个渲染...

  • 浏览器渲染流程及其性能优化分析

    渲染流程 一个页面呈现通过浏览器呈现到出来,会经过以下步骤 解析页面内容 构建DOM树 构建CSSOM树 合并DO...

  • 6组-刘宇昕-第二次作业

    Day2 作业主题 《第一课|教育模型解析:行业积累》 作业模板——教育行业摸底 例:1组-小嘉 拆解案例:白熊课...

  • 【干货】回流与重绘

    一、页面构建过程 从上面这个图上,我们可以看到,浏览器渲染过程如下: 解析HTML,生成DOM树,解析CSS,生成...

  • 第二次作业-美瞳

    《第一课|教育模型解析:行业积累》 5组-美瞳 拆解案例:豌豆思维 一、营销模型 1、模型框架:0元/9.9元转5...

网友评论

      本文标题:构建更加丰富的页面=>案例解析1

      本文链接:https://www.haomeiwen.com/subject/slkmgdtx.html