鴻蒙OS開發:【一次開發,多端部署】(一多天氣)項目

一多天氣

介紹

本示例展示一個天氣應用界面,包括首頁、城市管理、添加城市、更新時間彈窗,體現一次開發,多端部署的能力。

1.本示例參考一次開發,多端部署的指導,主要使用響應式布局的柵格斷點系統實現在不同尺寸窗口界面上不同的顯示效果。

2.使用[SideBarContainer]實現側邊欄功能。

3.使用[柵格容器組件]實現界面內容的分割和展示。

4.使用Canvas和CanvasRenderingContext2D完成空氣質量和日出月落圖的曲線繪制。

開發前請熟悉鴻蒙開發指導文檔gitee.com/li-shizhen-skin/harmony-os/blob/master/README.md點擊或者復制轉到。

效果預覽

image.png

使用說明:

1.啟動應用后,首頁展示已添加城市的天氣信息,默認展示2個城市,左右滑動可以切換城市,在LG設備上,默認顯示側邊欄,側邊欄顯示時,右側內容區占2/3,側邊欄隱藏時,內容區自動鋪滿界面。

2.在支持窗口自由拖拽的設備上,拖拽窗口大小,可以分別實現拖動到最大窗口側邊欄顯示(點擊側邊欄控制按鈕可以隱藏和顯示側邊欄),拖動窗口縮小到MD大小時側邊欄和側邊欄控制按鈕隱藏。

3.在支持窗口自由拖拽的設備上,拖拽窗口大小,天氣內容區跟隨窗口大小會自動換行顯示。

4.點擊右上角菜單按鈕,在菜單中點擊更新時間,彈出更新時間彈窗,沒有功能,此處只做展示,在平板設備上顯示2列,在小屏設備上顯示一列。

5.點擊右上角菜單按鈕,在菜單中點擊管理城市,進入管理城市界面,展示已添加的城市,在平板設備上顯示2列,在小屏設備上顯示一列。

6.點擊管理城市界面的添加城市,進入添加城市界面,已添加的城市不可點擊,未添加的城市點擊可以添加并返回管理城市界面顯示。

工程目錄

/code/SuperFeature/MultiDeviceAppDev/Weather/product/default
└─src├─main│  ││  ├─ets│  │  ├─Application│  │  │      MyAbilityStage.ts          //自定義ability│  │  ││  │  ├─common                          //公共資源庫│  │  ├─feature│  │  │      AirQualityFeature.ts       //空氣繪畫│  │  │      SunCanvasFeature.ts        //晴天繪畫│  │  ││  │  ├─MainAbility│  │  │      MainAbility.ts             //主窗口│  │  ││  │  └─pages│  │      │  AddCity.ets                //添加城市│  │      │  CityList.ets               //城市列表│  │      │  Home.ets                   //入口│  │      ││  │      └─home│  │              AirQuality.ets         //空氣質量│  │              HomeContent.ets        //主頁面│  │              HoursWeather.ets       //每小時天氣組件│  │              IndexEnd.ets           //首頁尾 │  │              IndexHeader.ets        //首頁頭│  │              IndexTitleBar.ets      //首頁標題│  │              LifeIndex.ets          //生活建議│  │              MultidayWeather.ets    //天氣組件│  │              SideContent.ets        //側邊欄│  │              SunCanvas.ets          //晴天樣式│  │              UpdateTimeDialog.ets   //時間更新彈窗│  ││  └─resources                           //資源包                                             

具體實現

1、home.ets中引入SideContent()和homeContent()。
2、定義showSideBar來判斷是否展示側邊欄,定義mediaquery.MediaQueryListener媒體監聽器smListener、mdListener、lgListener。
3、在aboutToAppear調用mediaquery對界面進行監聽,[源碼參考]。

/** Copyright (c) 2022-2023 Huawei Device Co., Ltd.* Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at**     http://www.apache.org/licenses/LICENSE-2.0** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.* See the License for the specific language governing permissions and* limitations under the License.*/import mediaquery from '@ohos.mediaquery';import HomeContent from './home/HomeContent';import IndexTitleBar from './home/IndexTitleBar';import SideContent from './home/SideContent';import { CityListData, Style, getBg, getCityListWeatherData, Logger } from '@ohos/common';const TAG: string = 'Home';@Entry@Componentstruct Home {@StorageLink('isRefresh') @Watch('refreshChange') isRefresh: boolean = false;@StorageLink('swiperIndex') swiperIndex: number = 0;@State curBp: string = 'md';@State cityListWeatherData: CityListData[] = getCityListWeatherData();@State popupState: boolean = false;@State showSideBar: boolean = false;private smListener: mediaquery.MediaQueryListener;private mdListener: mediaquery.MediaQueryListener;private lgListener: mediaquery.MediaQueryListener;build() {SideBarContainer(SideBarContainerType.Embed) {SideContent({ showSideBar: $showSideBar }).height('100%')Column() {IndexTitleBar({ showSideBar: $showSideBar }).height(56)Swiper() {ForEach(this.cityListWeatherData, (item, index) => {HomeContent({ showSideBar: this.showSideBar, cityListData: item, index: index })}, item => item.city)}.id('swiper').padding({ left: Style.NORMAL_PADDING, right: Style.NORMAL_PADDING }).indicatorStyle({selectedColor: Color.White}).onChange(index => {this.swiperIndex = index;AppStorage.SetOrCreate('swiperIndex', this.swiperIndex);}).indicator(this.curBp !== 'lg').index(this.swiperIndex).loop(false).width('100%').layoutWeight(1)}.height('100%')}.height('100%').sideBarWidth('33.3%').minSideBarWidth('33.3%').maxSideBarWidth('33.3%').showControlButton(false).showSideBar(this.showSideBar).backgroundImageSize(ImageSize.Cover).backgroundImage(getBg(this.cityListWeatherData[this.swiperIndex].header.weatherType))}aboutToAppear() {this.smListener = mediaquery.matchMediaSync('(320vp<width<=600vp)');this.smListener.on("change", this.isBreakpointSM);this.mdListener = mediaquery.matchMediaSync('(600vp<width<=840vp)');this.mdListener.on("change", this.isBreakpointMD);this.lgListener = mediaquery.matchMediaSync('(840vp<width)');this.lgListener.on("change", this.isBreakpointLG);}aboutToDisappear() {this.smListener.off("change", this.isBreakpointSM);this.mdListener.off("change", this.isBreakpointMD);this.lgListener.off("change", this.isBreakpointLG);}isBreakpointSM = (mediaQueryResult) => {if (mediaQueryResult.matches) {this.curBp = 'sm';this.showSideBar = false;AppStorage.SetOrCreate('curBp', this.curBp);}Logger.info(TAG, `this.curBp = ${this.curBp}`);}isBreakpointMD = (mediaQueryResult) => {if (mediaQueryResult.matches) {this.curBp = 'md';this.showSideBar = false;AppStorage.SetOrCreate('curBp', this.curBp);}Logger.info(TAG, `this.curBp = ${this.curBp}`);}isBreakpointLG = (mediaQueryResult) => {if (mediaQueryResult.matches) {if (this.curBp !== 'lg') {this.showSideBar = true;}this.curBp = 'lg';AppStorage.SetOrCreate('curBp', this.curBp);}Logger.info(TAG, `this.curBp = ${this.curBp}`);}refreshChange() {Logger.info(TAG, `refreshChange}`);if (this.isRefresh) {this.cityListWeatherData = getCityListWeatherData();AppStorage.SetOrCreate('isRefresh', false);}Logger.info(TAG, `refreshChange, this.cityListWeatherData.length = ${this.cityListWeatherData.length}`);}}`HarmonyOS與OpenHarmony鴻蒙文檔籽料:mau123789是v直接拿`

搜狗高速瀏覽器截圖20240326151450.png

4、監聽到當前屏幕大小,調用this.isBreakpoint斷點,對curBp、showSideBar進行賦值,[源碼參考]。

/** Copyright (c) 2022-2023 Huawei Device Co., Ltd.* Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at**     http://www.apache.org/licenses/LICENSE-2.0** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.* See the License for the specific language governing permissions and* limitations under the License.*/import mediaquery from '@ohos.mediaquery';import HomeContent from './home/HomeContent';import IndexTitleBar from './home/IndexTitleBar';import SideContent from './home/SideContent';import { CityListData, Style, getBg, getCityListWeatherData, Logger } from '@ohos/common';const TAG: string = 'Home';@Entry@Componentstruct Home {@StorageLink('isRefresh') @Watch('refreshChange') isRefresh: boolean = false;@StorageLink('swiperIndex') swiperIndex: number = 0;@State curBp: string = 'md';@State cityListWeatherData: CityListData[] = getCityListWeatherData();@State popupState: boolean = false;@State showSideBar: boolean = false;private smListener: mediaquery.MediaQueryListener;private mdListener: mediaquery.MediaQueryListener;private lgListener: mediaquery.MediaQueryListener;build() {SideBarContainer(SideBarContainerType.Embed) {SideContent({ showSideBar: $showSideBar }).height('100%')Column() {IndexTitleBar({ showSideBar: $showSideBar }).height(56)Swiper() {ForEach(this.cityListWeatherData, (item, index) => {HomeContent({ showSideBar: this.showSideBar, cityListData: item, index: index })}, item => item.city)}.id('swiper').padding({ left: Style.NORMAL_PADDING, right: Style.NORMAL_PADDING }).indicatorStyle({selectedColor: Color.White}).onChange(index => {this.swiperIndex = index;AppStorage.SetOrCreate('swiperIndex', this.swiperIndex);}).indicator(this.curBp !== 'lg').index(this.swiperIndex).loop(false).width('100%').layoutWeight(1)}.height('100%')}.height('100%').sideBarWidth('33.3%').minSideBarWidth('33.3%').maxSideBarWidth('33.3%').showControlButton(false).showSideBar(this.showSideBar).backgroundImageSize(ImageSize.Cover).backgroundImage(getBg(this.cityListWeatherData[this.swiperIndex].header.weatherType))}aboutToAppear() {this.smListener = mediaquery.matchMediaSync('(320vp<width<=600vp)');this.smListener.on("change", this.isBreakpointSM);this.mdListener = mediaquery.matchMediaSync('(600vp<width<=840vp)');this.mdListener.on("change", this.isBreakpointMD);this.lgListener = mediaquery.matchMediaSync('(840vp<width)');this.lgListener.on("change", this.isBreakpointLG);}aboutToDisappear() {this.smListener.off("change", this.isBreakpointSM);this.mdListener.off("change", this.isBreakpointMD);this.lgListener.off("change", this.isBreakpointLG);}isBreakpointSM = (mediaQueryResult) => {if (mediaQueryResult.matches) {this.curBp = 'sm';this.showSideBar = false;AppStorage.SetOrCreate('curBp', this.curBp);}Logger.info(TAG, `this.curBp = ${this.curBp}`);}isBreakpointMD = (mediaQueryResult) => {if (mediaQueryResult.matches) {this.curBp = 'md';this.showSideBar = false;AppStorage.SetOrCreate('curBp', this.curBp);}Logger.info(TAG, `this.curBp = ${this.curBp}`);}isBreakpointLG = (mediaQueryResult) => {if (mediaQueryResult.matches) {if (this.curBp !== 'lg') {this.showSideBar = true;}this.curBp = 'lg';AppStorage.SetOrCreate('curBp', this.curBp);}Logger.info(TAG, `this.curBp = ${this.curBp}`);}refreshChange() {Logger.info(TAG, `refreshChange}`);if (this.isRefresh) {this.cityListWeatherData = getCityListWeatherData();AppStorage.SetOrCreate('isRefresh', false);}Logger.info(TAG, `refreshChange, this.cityListWeatherData.length = ${this.cityListWeatherData.length}`);}}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/bicheng/18237.shtml
繁體地址,請注明出處:http://hk.pswp.cn/bicheng/18237.shtml
英文地址,請注明出處:http://en.pswp.cn/bicheng/18237.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

【Qt 學習筆記】Qt窗口 | 工具欄 | QToolBar的使用及說明

博客主頁&#xff1a;Duck Bro 博客主頁系列專欄&#xff1a;Qt 專欄關注博主&#xff0c;后期持續更新系列文章如果有錯誤感謝請大家批評指出&#xff0c;及時修改感謝大家點贊&#x1f44d;收藏?評論? Qt窗口 | 工具欄 | QToolBar的使用及說明 文章編號&#xff1a;Qt 學習…

怎么看智慧城市的發展?

智慧城市&#xff0c;就像一個擁有高度智慧和感知能力的未來城市居民&#xff0c;正在不斷地學習、適應和進化。它通過無數的眼睛&#xff08;傳感器&#xff09;和耳朵&#xff08;數據收集設備&#xff09;來觀察和傾聽城市的脈動&#xff0c;通過強大的大腦&#xff08;數據…

opencv文檔py_contours示例整理

文章目錄 目錄說明contours_begin目標什么是輪廓?如何畫等高線?輪廓逼近法contour_features目標1.Moments 時刻2. Contour Area 輪廓面積3. Contour Perimeter 輪廓周長4. Contour Approximation 輪廓近似5. Convex Hull 凸包6. Checking Convexity 檢查凸性7. Bounding Rect…

B2118 驗證子串

驗證子串 題目描述 輸入兩個字符串&#xff0c;驗證其中一個串是否為另一個串的子串。 輸入格式 兩行&#xff0c;每行一個字符串。 輸出格式 若第一個串 s 1 s_1 s1? 是第二個串 s 2 s_2 s2? 的子串&#xff0c;則輸出(s1) is substring of (s2)&#xff1b; 否則&…

Python并發與異步編程

Python的并發與異步編程是兩個不同的概念&#xff0c;但它們經常一起使用&#xff0c;以提高程序的性能和響應能力。以下是對這兩個概念的詳細講解&#xff1a; 并發編程 (Concurrency) 并發編程是指在程序中同時執行多個任務的能力。Python提供了幾種實現并發的機制&#xff…

嵌入式進階——RTC時鐘

&#x1f3ac; 秋野醬&#xff1a;《個人主頁》 &#x1f525; 個人專欄:《Java專欄》《Python專欄》 ??心若有所向往,何懼道阻且長 文章目錄 RTC時鐘原理圖PCF8563寄存器控制與狀態寄存器 設備地址I2C環境初始化RTC寄存器數據讀取RTC寄存器數據寫入RTC鬧鐘設置RTC定時器設置…

2024.5.28晚訓題解

提前預告&#xff0c;市賽初中組會考算法題&#xff0c;應該會有兩道模板題 比如DFS BFS 二分 簡單動態規劃&#xff0c;雖然我們沒學多久&#xff0c;但是模板題你還是要會寫的 A題 編輯距離 動態規劃 注意多組輸入 #include<iostream> using namespace std; int dp[1…

9、C#【進階】特性

特性 文章目錄 1、特性概念2、自定義特性 Attribute3、特性的使用4、限制自定義特性的使用范圍5、系統自帶特性1、過時特性2、調用者信息特性3、條件編譯特性4、外部dll包函數特性 1、特性概念 特性是一種允許我們向程序的程序集添加元數據的語言結構 它是用于保存程序機構信息…

【機器學習300問】103、簡單的經典卷積神經網絡結構設計成什么樣?以LeNet-5為例說明。

一個簡單的經典CNN網絡結構由&#xff1a;輸入層、卷積層、池化層、全連接層和輸出層&#xff0c;這五種神經網絡層結構組成。它最最經典的實例是LeNet-5&#xff0c;它最早被設計用于手寫數字識別任務&#xff0c;包含兩個卷積層、兩個池化層、幾個全連接層&#xff0c;以及最…

ansible批量漏洞升級openssh版本

1、ansible宿主機準備好環境&#xff0c;并寫好hosts文件 [rootoxidized ansible]# cat hosts [all] 10.10.200.33 10.10.200.34 10.10.200.35跑playbook之前記得提前發送秘鑰 ssh-copy-id 10.10.200.33/34/352、下載好安裝包&#xff0c;然后編寫yml [rootoxidized ansible]…

【實用的 IDEA 配置和操作技巧總結】

前置知識 IDEA的設置快捷鍵為ctrlalts鍵&#xff0c;后文介紹IDEA常見的配置就不再贅述這一點了。 基礎配置 取消默認打開上次項目 日常開發都會打開不同的項目&#xff0c;初次安裝IDEA之后&#xff0c;每次打開IDEA都會開啟上一次啟動的項目&#xff0c;所以我們需要進入設…

0基礎學習Mybatis系列數據庫操作框架——Mysql的Geometry數據處理之WKB方案

大綱 序列化反序列化完整TypeHandlerSQL XML完整XML Mapper測試代碼代碼 在《0基礎學習Mybatis系列數據庫操作框架——Mysql的Geometry數據處理之WKT方案》中&#xff0c;我們介紹WTK方案的優點&#xff0c;也感受到它的繁瑣和缺陷。比如&#xff1a; 需要借助ST_GeomFromText…

element+ 引入圖標報錯 Failed to resolve import “@element-plus/icons-vue“ from “

element 引入圖標報錯 Internal server error: Failed to resolve import “element-plus/icons-vue” from “src\components\TimeLine.vue”. Does the file exist? 原因&#xff1a;element-plus需要單獨引入 icons 文檔 pnpm install element-plus/icons-vue之后就可以…

350種類型、10W+量級的API,企業應該怎么管?

忽如一夜春風來&#xff0c;萬物皆可API。 在互聯網時代&#xff0c;API無處不在&#xff1a;企業對外開放的數據、服務和業務能力&#xff0c;以API的形式提供給合作方&#xff1b;企業內部應用與應用、App與App之間的通信&#xff0c;通過API進行&#xff1b;甚至應用內部的…

php 連接sqlserver步驟

1.首先要確定使用的是sqlserver的哪個版本&#xff0c;比如sqlserver2012 2.確定服務器是64位還是32位的 3.確認一下使用php的哪個版本&#xff0c;比如php7.1 SQL Server 的 Microsoft PHP 驅動程序 Microsoft Drivers for PHP 支持矩陣 - PHP drivers for SQL Server | Mi…

Flutter 中的 CupertinoTabView 小部件:全面指南

Flutter 中的 CupertinoTabView 小部件&#xff1a;全面指南 在 Flutter 中&#xff0c;CupertinoTabView 是 Cupertino 組件庫中的一個 widget&#xff0c;它用于創建 iOS 風格的標簽頁視圖。這個 widget 通常與 CupertinoTabScaffold 結合使用&#xff0c;提供了一個底部帶有…

怎么做好客戶信息管理?

根據Forrester的調查表示&#xff0c;客戶滿意度的影響可能會使某些行業的收入每年增加高達 10 億美元。而提升客戶滿意度的關鍵環節便是做好客戶信息管理。但企業在進行客戶信息管理中往往會遇到以下問題&#xff1a; 客戶信息亂&#xff1a;客戶信息存在各個 Excel表格、個人…

PMP報考條件怎么查詢?如何判定自己是否符合條件?

PMP報考條件在PMI官網上就可以查詢&#xff0c;PMP報考條件只需要符合項目管理培訓經歷和項目管理經驗兩個方面的要求即可&#xff0c;大家可以對照下方的規定判斷自己是否符合PMP報名條件 PMP報考條件 以下是PMI&#xff08;中國&#xff09;官網對于PMP報名條件的規定&…

優秀的數據分析師需要具備哪些?

在數據驅動的時代&#xff0c;數據分析師的角色越來越被重視。本文將探討優秀數據分析師必備的三大核心能力&#xff0c;并通過實際案例說明如何將這些能力轉化為業務價值&#xff0c;幫助你在職業道路上更進一步。 在數字化迅速發展的今天&#xff0c;數據分析師扮演著極其重要…

ubuntu strace命令

strace 是 Linux 系統中的一個調試工具&#xff0c;用于跟蹤并記錄系統調用&#xff08;system calls&#xff09;和信號&#xff08;signals&#xff09;。在 Ubuntu 中&#xff0c;strace 命令可以幫助開發者和系統管理員了解一個程序在運行時如何與操作系統內核進行交互&…