在 HarmonyOS Next 中开发一个简单的通用头部导航栏

1作者: flfljh7 个月前
# 在 HarmonyOS Next 中开发一个简单的通用标题导航栏 <p>在日常的页面开发中,大多数页面都需要一个标题栏来显示导航控件和页面信息。为每个页面重复编写这些代码效率低下,并且会导致实现不一致。本指南演示了如何创建一个可重用的标题组件。</p> <p>## 步骤 1:创建 NavBar 组件</p> <p>创建一个新的 ArkTS 文件,使用 `@Component` 装饰的自定义组件:</p> ``` import Utils from &quot;..&#x2F;..&#x2F;..&#x2F;common&#x2F;utils&#x2F;Utils&quot;; @Component export struct NavBar { &#x2F;&#x2F; 组件实现将在此处 } ``` <p>## 步骤 2:实现组件属性和 UI</p> <p>添加属性并构建标题 UI:</p> ``` import Utils from &quot;..&#x2F;..&#x2F;..&#x2F;common&#x2F;utils&#x2F;Utils&quot;; @Component export struct NavBar { @Prop title: string = &#x27;&#x27;; &#x2F;&#x2F; 标题文本 @Prop backImg: string = &#x27;&#x27;; &#x2F;&#x2F; 自定义返回按钮图标路径 @Prop bgColor: string = &#x27;#FFFFFF&#x27;;&#x2F;&#x2F; 标题背景颜色 @Prop customBack?: () =&gt; void; &#x2F;&#x2F; 自定义返回按钮处理程序 @Prop mode: string = &#x27;center&#x27;; &#x2F;&#x2F; 标题对齐方式:&#x27;center&#x27; 或 &#x27;left&#x27; build() { Row() { &#x2F;&#x2F; 返回按钮部分 Row() { Image(this.backImg || Utils.getImgPath(&#x27;home&#x2F;adult_page_back_black.png&#x27;)) .width(Utils.getVp(48)) .height(Utils.getVp(48)) .objectFit(ImageFit.Cover) } .onClick(() =&gt; { &#x2F;&#x2F; 如果提供了自定义处理程序,则使用自定义处理程序,否则使用默认的返回导航 this.customBack ? this.customBack() : router.back(); }) &#x2F;&#x2F; 标题部分 Row() { Text(this.title) .fontColor(&#x27;#191B27&#x27;) .fontSize(Utils.getVp(33)) .fontWeight(FontWeight.Bold) .textAlign(this.mode === &#x27;center&#x27; ? TextAlign.Center : TextAlign.Start) .width(&#x27;100%&#x27;) } .margin({ left: this.mode === &#x27;center&#x27; ? 0 : Utils.getVp(20) }) .flexShrink(1) &#x2F;&#x2F; 允许收缩以适应内容 .height(&#x27;100%&#x27;) } .width(&#x27;100%&#x27;) .padding({ left: Utils.getVp(32), right: Utils.getVp(32) }) .height(Utils.getVp(88)) .backgroundColor(this.bgColor) } } ``` <p>## 步骤 3:在父视图中使用组件</p> <p>### 居中对齐标题示例:</p> ``` import { NavBar } from &#x27;.&#x2F;component&#x2F;NavBar&#x27;; @Entry @Component struct ParentPage { build() { Column() { NavBar({ title: &#x27;页面标题&#x27;, mode: &#x27;center&#x27; }) &#x2F;&#x2F; 页面内容在这里 } .width(&#x27;100%&#x27;) .height(&#x27;100%&#x27;) } } ``` <p>### 左对齐标题示例:</p> ``` import { NavBar } from &#x27;.&#x2F;component&#x2F;NavBar&#x27;; @Entry @Component struct ParentPage { build() { Column() { NavBar({ title: &#x27;页面标题&#x27;, mode: &#x27;left&#x27; }) &#x2F;&#x2F; 页面内容在这里 } .width(&#x27;100%&#x27;) .height(&#x27;100%&#x27;) } } ``` <p>## 自定义选项</p> <p>1. *自定义图标*:使用图像路径传递 `backImg` 属性</p> ``` NavBar({ title: &#x27;设置&#x27;, backImg: Utils.getImgPath(&#x27;icons&#x2F;custom_back.png&#x27;) }) ``` <p>2. *自定义背景*:更改标题颜色</p> ``` NavBar({ title: &#x27;个人资料&#x27;, bgColor: &#x27;#F5F5F5&#x27; }) ``` <p>3. *自定义返回操作*:覆盖默认导航</p> ``` NavBar({ title: &#x27;结账&#x27;, customBack: () =&gt; { &#x2F;* 自定义逻辑 *&#x2F; } }) ``` <p>## 主要特性</p> - *响应式设计*:使用 `Utils.getVp()` 适应不同的屏幕尺寸 - *灵活的布局*:标题对齐选项(居中/左对齐) - *可定制*:支持自定义图标、颜色和行为 - *一致的 UI*:确保应用程序中统一的标题外观 - *易于集成*:基于 props 的简单配置 <p>此实现提供了一个可重用的、可定制的标题组件,消除了代码重复,并确保在整个 HarmonyOS Next 应用程序中提供一致的导航体验。</p>
查看原文
# Developing a Simple Universal Header Navigation Bar in HarmonyOS Next<p>In daily page development, most pages require a header to display navigation controls and page information. Repeating this code for every page is inefficient and leads to inconsistent implementations. This guide demonstrates how to create a reusable header component.<p>## Step 1: Create the NavBar Component<p>Create a new ArkTS file with a `@Component` decorated custom component:<p>``` import Utils from &quot;..&#x2F;..&#x2F;..&#x2F;common&#x2F;utils&#x2F;Utils&quot;;<p>@Component export struct NavBar { &#x2F;&#x2F; Component implementation will go here } ```<p>## Step 2: Implement Component Properties and UI<p>Add properties and build the header UI:<p>``` import Utils from &quot;..&#x2F;..&#x2F;..&#x2F;common&#x2F;utils&#x2F;Utils&quot;;<p>@Component export struct NavBar { @Prop title: string = &#x27;&#x27;; &#x2F;&#x2F; Header title text @Prop backImg: string = &#x27;&#x27;; &#x2F;&#x2F; Custom back button icon path @Prop bgColor: string = &#x27;#FFFFFF&#x27;;&#x2F;&#x2F; Header background color @Prop customBack?: () =&gt; void; &#x2F;&#x2F; Custom back button handler @Prop mode: string = &#x27;center&#x27;; &#x2F;&#x2F; Title alignment: &#x27;center&#x27; or &#x27;left&#x27;<p><pre><code> build() { Row() { &#x2F;&#x2F; Back button section Row() { Image(this.backImg || Utils.getImgPath(&#x27;home&#x2F;adult_page_back_black.png&#x27;)) .width(Utils.getVp(48)) .height(Utils.getVp(48)) .objectFit(ImageFit.Cover) } .onClick(() =&gt; { &#x2F;&#x2F; Use custom handler if provided, else default back navigation this.customBack ? this.customBack() : router.back(); }) &#x2F;&#x2F; Title section Row() { Text(this.title) .fontColor(&#x27;#191B27&#x27;) .fontSize(Utils.getVp(33)) .fontWeight(FontWeight.Bold) .textAlign(this.mode === &#x27;center&#x27; ? TextAlign.Center : TextAlign.Start) .width(&#x27;100%&#x27;) } .margin({ left: this.mode === &#x27;center&#x27; ? 0 : Utils.getVp(20) }) .flexShrink(1) &#x2F;&#x2F; Allow shrinking to fit content .height(&#x27;100%&#x27;) } .width(&#x27;100%&#x27;) .padding({ left: Utils.getVp(32), right: Utils.getVp(32) }) .height(Utils.getVp(88)) .backgroundColor(this.bgColor) }</code></pre> } ```<p>## Step 3: Using the Component in Parent Views<p>### Center-aligned Title Example:<p>``` import { NavBar } from &#x27;.&#x2F;component&#x2F;NavBar&#x27;;<p>@Entry @Component struct ParentPage { build() { Column() { NavBar({ title: &#x27;Page Title&#x27;, mode: &#x27;center&#x27; }) &#x2F;&#x2F; Page content here } .width(&#x27;100%&#x27;) .height(&#x27;100%&#x27;) } } ```<p>### Left-aligned Title Example:<p>``` import { NavBar } from &#x27;.&#x2F;component&#x2F;NavBar&#x27;;<p>@Entry @Component struct ParentPage { build() { Column() { NavBar({ title: &#x27;Page Title&#x27;, mode: &#x27;left&#x27; }) &#x2F;&#x2F; Page content here } .width(&#x27;100%&#x27;) .height(&#x27;100%&#x27;) } } ```<p>## Customization Options<p>1. *Custom Icons*: Pass `backImg` property with image path<p><pre><code> ``` NavBar({ title: &#x27;Settings&#x27;, backImg: Utils.getImgPath(&#x27;icons&#x2F;custom_back.png&#x27;) }) ``` </code></pre> 2. *Custom Background*: Change header color<p><pre><code> ``` NavBar({ title: &#x27;Profile&#x27;, bgColor: &#x27;#F5F5F5&#x27; }) ``` </code></pre> 3. *Custom Back Action*: Override default navigation<p><pre><code> ``` NavBar({ title: &#x27;Checkout&#x27;, customBack: () =&gt; { &#x2F;* Custom logic *&#x2F; } }) ``` </code></pre> ## Key Features<p>- *Responsive Design*: Adapts to different screen sizes using `Utils.getVp()` - *Flexible Layout*: Title alignment options (center&#x2F;left) - *Customizable*: Supports custom icons, colors, and behaviors - *Consistent UI*: Ensures uniform header appearance across application - *Easy Integration*: Simple props-based configuration<p>This implementation provides a reusable, customizable header component that eliminates code duplication and ensures consistent navigation experiences throughout your HarmonyOS Next application.