From a3f55b616941c7ba2f665b5355a640d0eef30b63 Mon Sep 17 00:00:00 2001 From: kangliang <2352009235@qq.com> Date: Sat, 6 Sep 2025 17:11:47 +0800 Subject: [PATCH 01/10] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/ets/components/ActionBarAdapter.ets | 60 +++++++++++++++++++ entry/src/main/ets/pages/Index.ets | 2 + 2 files changed, 62 insertions(+) create mode 100644 entry/src/main/ets/components/ActionBarAdapter.ets diff --git a/entry/src/main/ets/components/ActionBarAdapter.ets b/entry/src/main/ets/components/ActionBarAdapter.ets new file mode 100644 index 0000000..e33cb88 --- /dev/null +++ b/entry/src/main/ets/components/ActionBarAdapter.ets @@ -0,0 +1,60 @@ +/* + * Copyright (c) 2025 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 { HdsActionBar, ActionBarButton, ActionBarStyle } from '@kit.UIDesignKit' + +@Entry +@Component +struct ActionBarAdapter { + @State isExpand: boolean = true; + @State isPrimaryIconChanged: boolean = false; + + build() { + Column() { + // Compatibility judgment, 60000 is derived from the since field of the new interface M*10000 F*100 S. + if (deviceInfo.distributionOSApiVersion >= 50001) { + // Component that calls the API of version 6.0.0(20) + HdsActionBar({ + startButtons: [new ActionBarButton({ + baseIcon: $r('sys.symbol.stopwatch_fill') + })], + endButtons: [new ActionBarButton({ + baseIcon: $r('sys.symbol.mic_fill') + })], + primaryButton: new ActionBarButton({ + baseIcon: $r('sys.symbol.plus'), + altIcon: $r('sys.symbol.play_fill'), + onClick: () => { + this.isExpand = !this.isExpand; + this.isPrimaryIconChanged = !this.isPrimaryIconChanged; + } + }), + actionBarStyle: new ActionBarStyle({ + isPrimaryIconChanged: this.isPrimaryIconChanged + }), + isExpand: this.isExpand!! + }) + } else { + // Downgrading plan + // ... + } + } + .width('100%') + .height('100%') + .backgroundColor(0xF1F3F5) + .justifyContent(FlexAlign.Center) + .alignItems(HorizontalAlign.Center) + } +} \ No newline at end of file diff --git a/entry/src/main/ets/pages/Index.ets b/entry/src/main/ets/pages/Index.ets index 82684a2..8bb6047 100644 --- a/entry/src/main/ets/pages/Index.ets +++ b/entry/src/main/ets/pages/Index.ets @@ -15,6 +15,7 @@ import { ScrollComponentAdapter } from '../components/ScrollComponentAdapter'; import { TextComponentAdapter } from '../components/TextComponentAdapter'; +import { ActionBarAdapter } from '../components/ActionBarAdapter'; @Entry @Component @@ -23,6 +24,7 @@ struct Index { Column() { ScrollComponentAdapter() TextComponentAdapter() + ActionBarAdapter() } .height('100%') .width('100%') -- Gitee From 36338a1ec7f0391aa6806f55bf3ca56cdd234450 Mon Sep 17 00:00:00 2001 From: kangliang <2352009235@qq.com> Date: Sat, 6 Sep 2025 17:14:46 +0800 Subject: [PATCH 02/10] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 ++ entry/src/main/ets/components/ActionBarAdapter.ets | 3 ++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 169feeb..1655132 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,7 @@ ``` ├──entry/src/main/ets // 代码区 │ ├──components // 自定义组件 +│ │ ├──ActionBarAdapter.ets // HdsActionBar组件版本兼容示例 │ │ ├──ScrollComponentAdapter.ets // Scroll组件版本兼容示例 │ │ └──TextComponentAdapter.ets // Text组件版本兼容示例 │ ├──entryability @@ -25,6 +26,7 @@ * 通过@ohos.deviceInfo(设备信息)模块deviceInfo.sdkApiVersion属性来获取当前设备SDK版本,然后和目标版本进行比对。 * 以Text组件的marqueeOptions属性使用为例来展示API18的兼容,实现了跑马灯效果 * 以Scroll组件的maxZoomScale、minZoomScale和enableBouncesZoom为例子来展示API20的兼容,实现了图片缩放效果。 +* 以HdsActionBar组件的使用例子来展示API20的兼容,实现可以展开和收起的ActionBar效果。 ### 相关权限 不涉及 diff --git a/entry/src/main/ets/components/ActionBarAdapter.ets b/entry/src/main/ets/components/ActionBarAdapter.ets index e33cb88..a998d10 100644 --- a/entry/src/main/ets/components/ActionBarAdapter.ets +++ b/entry/src/main/ets/components/ActionBarAdapter.ets @@ -13,7 +13,8 @@ * limitations under the License. */ -import { HdsActionBar, ActionBarButton, ActionBarStyle } from '@kit.UIDesignKit' +import { HdsActionBar, ActionBarButton, ActionBarStyle } from '@kit.UIDesignKit'; +import { deviceInfo } from '@kit.BasicServicesKit'; @Entry @Component -- Gitee From d1d1f259d14547fc2c32423e62c5d44e91681c30 Mon Sep 17 00:00:00 2001 From: kangliang <2352009235@qq.com> Date: Sat, 6 Sep 2025 17:15:53 +0800 Subject: [PATCH 03/10] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- entry/src/main/ets/components/ActionBarAdapter.ets | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/entry/src/main/ets/components/ActionBarAdapter.ets b/entry/src/main/ets/components/ActionBarAdapter.ets index a998d10..3f266ec 100644 --- a/entry/src/main/ets/components/ActionBarAdapter.ets +++ b/entry/src/main/ets/components/ActionBarAdapter.ets @@ -16,9 +16,8 @@ import { HdsActionBar, ActionBarButton, ActionBarStyle } from '@kit.UIDesignKit'; import { deviceInfo } from '@kit.BasicServicesKit'; -@Entry @Component -struct ActionBarAdapter { +export struct ActionBarAdapter { @State isExpand: boolean = true; @State isPrimaryIconChanged: boolean = false; -- Gitee From 8b6570325015e3a500ccde2b560595980f42ae15 Mon Sep 17 00:00:00 2001 From: kangliang <2352009235@qq.com> Date: Sat, 6 Sep 2025 17:16:29 +0800 Subject: [PATCH 04/10] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- entry/src/main/ets/components/ActionBarAdapter.ets | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/entry/src/main/ets/components/ActionBarAdapter.ets b/entry/src/main/ets/components/ActionBarAdapter.ets index 3f266ec..680c6c9 100644 --- a/entry/src/main/ets/components/ActionBarAdapter.ets +++ b/entry/src/main/ets/components/ActionBarAdapter.ets @@ -24,7 +24,7 @@ export struct ActionBarAdapter { build() { Column() { // Compatibility judgment, 60000 is derived from the since field of the new interface M*10000 F*100 S. - if (deviceInfo.distributionOSApiVersion >= 50001) { + if (deviceInfo.distributionOSApiVersion >= 60000) { // Component that calls the API of version 6.0.0(20) HdsActionBar({ startButtons: [new ActionBarButton({ -- Gitee From 8c9ce48c41a36670bcef4e198f3d965a46d879a5 Mon Sep 17 00:00:00 2001 From: kangliang <2352009235@qq.com> Date: Sat, 6 Sep 2025 17:18:16 +0800 Subject: [PATCH 05/10] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- entry/src/main/ets/components/ActionBarAdapter.ets | 2 -- 1 file changed, 2 deletions(-) diff --git a/entry/src/main/ets/components/ActionBarAdapter.ets b/entry/src/main/ets/components/ActionBarAdapter.ets index 680c6c9..acfbd1d 100644 --- a/entry/src/main/ets/components/ActionBarAdapter.ets +++ b/entry/src/main/ets/components/ActionBarAdapter.ets @@ -51,9 +51,7 @@ export struct ActionBarAdapter { // ... } } - .width('100%') .height('100%') - .backgroundColor(0xF1F3F5) .justifyContent(FlexAlign.Center) .alignItems(HorizontalAlign.Center) } -- Gitee From 97aa8d0209cdc836256061804fbe6d45bf778eb4 Mon Sep 17 00:00:00 2001 From: kangliang <2352009235@qq.com> Date: Sat, 6 Sep 2025 17:50:03 +0800 Subject: [PATCH 06/10] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/ets/components/ActionBarAdapter.ets | 46 ++++++++++++++++++- 1 file changed, 44 insertions(+), 2 deletions(-) diff --git a/entry/src/main/ets/components/ActionBarAdapter.ets b/entry/src/main/ets/components/ActionBarAdapter.ets index acfbd1d..1d475e9 100644 --- a/entry/src/main/ets/components/ActionBarAdapter.ets +++ b/entry/src/main/ets/components/ActionBarAdapter.ets @@ -16,8 +16,9 @@ import { HdsActionBar, ActionBarButton, ActionBarStyle } from '@kit.UIDesignKit'; import { deviceInfo } from '@kit.BasicServicesKit'; +@Entry @Component -export struct ActionBarAdapter { +struct TestPage3 { @State isExpand: boolean = true; @State isPrimaryIconChanged: boolean = false; @@ -48,10 +49,51 @@ export struct ActionBarAdapter { }) } else { // Downgrading plan - // ... + Row({ space: 25 }) { + if (this.isExpand) { + Button({ type: ButtonType.Circle }) { + SymbolGlyph($r('sys.symbol.stopwatch_fill')) + .fontSize(24) + .fontColor([$r('sys.color.font_secondary')]) + } + .aspectRatio(1) + .height(45) + .backgroundColor($r('sys.color.background_secondary')) + .margin({ left: 10 }) + } + + Button({ type: ButtonType.Circle }) { + SymbolGlyph(this.isExpand ? $r('sys.symbol.plus') : $r('sys.symbol.play_fill')) + .fontSize(24) + .fontColor([$r('sys.color.white')]) + } + .aspectRatio(1) + .height(55) + .backgroundColor($r('sys.color.brand')) + + .onClick(() => { + this.isExpand = !this.isExpand; + }) + + if (this.isExpand) { + Button({ type: ButtonType.Circle }) { + SymbolGlyph($r('sys.symbol.mic_fill')) + .fontSize(24) + .fontColor([$r('sys.color.font_secondary')]) + } + .aspectRatio(1) + .height(45) + .backgroundColor($r('sys.color.background_secondary')) + .margin({ right: 10 }) + } + } + .backgroundColor($r('sys.color.background_primary')) + .borderRadius(30) } } + .width('100%') .height('100%') + .backgroundColor(0xF1F3F5) .justifyContent(FlexAlign.Center) .alignItems(HorizontalAlign.Center) } -- Gitee From f82619d0a2a551ec7a6f540e96f275c66812d56f Mon Sep 17 00:00:00 2001 From: kangliang <2352009235@qq.com> Date: Sat, 6 Sep 2025 17:51:15 +0800 Subject: [PATCH 07/10] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- entry/src/main/ets/components/ActionBarAdapter.ets | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/entry/src/main/ets/components/ActionBarAdapter.ets b/entry/src/main/ets/components/ActionBarAdapter.ets index 1d475e9..74ab5df 100644 --- a/entry/src/main/ets/components/ActionBarAdapter.ets +++ b/entry/src/main/ets/components/ActionBarAdapter.ets @@ -16,9 +16,8 @@ import { HdsActionBar, ActionBarButton, ActionBarStyle } from '@kit.UIDesignKit'; import { deviceInfo } from '@kit.BasicServicesKit'; -@Entry @Component -struct TestPage3 { +export struct ActionBarAdapter { @State isExpand: boolean = true; @State isPrimaryIconChanged: boolean = false; -- Gitee From 25d375a5fc1428e5c690c1eb248aca59b4720830 Mon Sep 17 00:00:00 2001 From: kangliang <2352009235@qq.com> Date: Sat, 6 Sep 2025 17:51:55 +0800 Subject: [PATCH 08/10] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- entry/src/main/ets/components/ActionBarAdapter.ets | 2 -- 1 file changed, 2 deletions(-) diff --git a/entry/src/main/ets/components/ActionBarAdapter.ets b/entry/src/main/ets/components/ActionBarAdapter.ets index 74ab5df..3c468c8 100644 --- a/entry/src/main/ets/components/ActionBarAdapter.ets +++ b/entry/src/main/ets/components/ActionBarAdapter.ets @@ -91,8 +91,6 @@ export struct ActionBarAdapter { } } .width('100%') - .height('100%') - .backgroundColor(0xF1F3F5) .justifyContent(FlexAlign.Center) .alignItems(HorizontalAlign.Center) } -- Gitee From 168a18a029b0dad5ac41b22e30b771e7ca6f564f Mon Sep 17 00:00:00 2001 From: kangliang <2352009235@qq.com> Date: Sat, 6 Sep 2025 18:44:39 +0800 Subject: [PATCH 09/10] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 1655132..cc94cab 100644 --- a/README.md +++ b/README.md @@ -23,7 +23,7 @@ 安装应用之后,进入首页。 ### 实现说明 -* 通过@ohos.deviceInfo(设备信息)模块deviceInfo.sdkApiVersion属性来获取当前设备SDK版本,然后和目标版本进行比对。 +* 通过@ohos.deviceInfo(设备信息)模块deviceInfo.sdkApiVersion和deviceInfo.distributionOSApiVersion属性来获取当前设备SDK版本,然后和目标版本进行比对。 * 以Text组件的marqueeOptions属性使用为例来展示API18的兼容,实现了跑马灯效果 * 以Scroll组件的maxZoomScale、minZoomScale和enableBouncesZoom为例子来展示API20的兼容,实现了图片缩放效果。 * 以HdsActionBar组件的使用例子来展示API20的兼容,实现可以展开和收起的ActionBar效果。 -- Gitee From d70148f2b6e0f45d51c1ac59970d397a1dba033d Mon Sep 17 00:00:00 2001 From: kangliang <2352009235@qq.com> Date: Sat, 6 Sep 2025 18:46:12 +0800 Subject: [PATCH 10/10] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index cc94cab..22a45f2 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # 多API版本兼容示例 ### 介绍 -通过@ohos.deviceInfo(设备信息)模块deviceInfo.sdkApiVersion获取系统SDK版本,以Text组件和Scroll组件在API 18和API 20的兼容为例,介绍多版本适配的实现方法。 +通过@ohos.deviceInfo(设备信息)模块deviceInfo.sdkApiVersion和deviceInfo.distributionOSApiVersion获取系统SDK版本,以Text、Scroll和HdsActionBar组件在API 18和API 20的兼容为例,介绍多版本适配的实现方法。 ### 工程目录 -- Gitee