<HarmonyOS第一課>構(gòu)建更加豐富的頁面=>案例解析1

官網(wǎng)案例

gitee源碼地址

相關(guān)概念

  build() {
    Column() {
      //標(biāo)題
      this.titleBar()
      //內(nèi)容區(qū)域
      TargetInformation(
           ...
      )
      //子目標(biāo)
      TargetList(
            ...
      )
  }

一、標(biāo)題

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('工作目標(biāo)')
      .fontSize(24)
      .fontWeight(FontWeight.Bold)
      .textAlign(TextAlign.Start)
      .width('86.7%')
      .margin({top:20,bottom:10})

  }
}

首先卸勺,實現(xiàn)了標(biāo)題的功能


image.png

二祷杈、內(nèi)容區(qū)域

新建文件夾view翰苫,新建TargetInformation類


image.png

在index.ets中導(dǎo)入TargetInformation

    Column() {
      //標(biāo)題
      this.titleBar()
      //內(nèi)容區(qū)域
      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('第一季度運營目標(biāo)')
          .fontSize(18)
          .fontWeight(FontWeight.Bold)
          .width('86.7%')
        Text('第一季度運營目標(biāo)')
          .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文件夾下

=> 繼續(xù)完善內(nèi)容區(qū)域

@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('第一季度運營目標(biāo)')
          .fontSize(18)
          .fontWeight(FontWeight.Bold)
          .width('86.7%')
        Text('實現(xiàn)用戶量與用戶活躍度提升')
          .opacityTextStyle()
          .fontSize(15)
          .margin({top:12})
      }
      .margin({left:'5%'})
      .alignItems(HorizontalAlign.Start)
    }
  }

  @Builder OverallProgress(){
    Row(){
      Column(){
        Text('整體進(jìn)度')
          .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

至此笛求,我們完成了內(nèi)容區(qū)域,但是會發(fā)現(xiàn)數(shù)據(jù)都是寫死的糕簿,我們從index.ets傳過來探入。

修改index.ets中的代碼

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

  build() {
    Column() {
      //標(biāo)題
     ...
      //內(nèi)容區(qū)域
      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

至此,內(nèi)容區(qū)域大功告成懂诗,我們只需要從index.ets中修改latestUpdateDate蜂嗽、totalTasksNumber、completedTasksNumber即可殃恒。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末植旧,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子离唐,更是在濱河造成了極大的恐慌病附,老刑警劉巖,帶你破解...
    沈念sama閱讀 216,919評論 6 502
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件亥鬓,死亡現(xiàn)場離奇詭異完沪,居然都是意外死亡,警方通過查閱死者的電腦和手機嵌戈,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,567評論 3 392
  • 文/潘曉璐 我一進(jìn)店門覆积,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人熟呛,你說我怎么就攤上這事宽档。” “怎么了庵朝?”我有些...
    開封第一講書人閱讀 163,316評論 0 353
  • 文/不壞的土叔 我叫張陵吗冤,是天一觀的道長又厉。 經(jīng)常有香客問我,道長欣孤,這世上最難降的妖魔是什么馋没? 我笑而不...
    開封第一講書人閱讀 58,294評論 1 292
  • 正文 為了忘掉前任,我火速辦了婚禮降传,結(jié)果婚禮上篷朵,老公的妹妹穿的比我還像新娘。我一直安慰自己婆排,他們只是感情好声旺,可當(dāng)我...
    茶點故事閱讀 67,318評論 6 390
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著段只,像睡著了一般腮猖。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上赞枕,一...
    開封第一講書人閱讀 51,245評論 1 299
  • 那天澈缺,我揣著相機與錄音,去河邊找鬼炕婶。 笑死姐赡,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的柠掂。 我是一名探鬼主播项滑,決...
    沈念sama閱讀 40,120評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼涯贞!你這毒婦竟也來了枪狂?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,964評論 0 275
  • 序言:老撾萬榮一對情侶失蹤宋渔,失蹤者是張志新(化名)和其女友劉穎州疾,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體皇拣,經(jīng)...
    沈念sama閱讀 45,376評論 1 313
  • 正文 獨居荒郊野嶺守林人離奇死亡孝治,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,592評論 2 333
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了审磁。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,764評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡岂座,死狀恐怖态蒂,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情费什,我是刑警寧澤钾恢,帶...
    沈念sama閱讀 35,460評論 5 344
  • 正文 年R本政府宣布手素,位于F島的核電站,受9級特大地震影響瘩蚪,放射性物質(zhì)發(fā)生泄漏泉懦。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,070評論 3 327
  • 文/蒙蒙 一疹瘦、第九天 我趴在偏房一處隱蔽的房頂上張望崩哩。 院中可真熱鬧,春花似錦言沐、人聲如沸邓嘹。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,697評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽汹押。三九已至,卻和暖如春起便,著一層夾襖步出監(jiān)牢的瞬間棚贾,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,846評論 1 269
  • 我被黑心中介騙來泰國打工榆综, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留妙痹,地道東北人。 一個月前我還...
    沈念sama閱讀 47,819評論 2 370
  • 正文 我出身青樓奖年,卻偏偏與公主長得像细诸,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子陋守,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,665評論 2 354