HarmonyOS鸿蒙Next中常见的标题栏布局方案,请提供源码和步骤讲解

HarmonyOS鸿蒙Next中常见的标题栏布局方案,请提供源码和步骤讲解

鸿蒙中常见的标题栏布局方案,请提供源码和步骤讲解

3 回复

二、标题栏布局

鸿蒙中常见的标题栏:矩形区域,左边是返回按钮,右边是问号帮助按钮,中间是标题文字。

一般使用Flex,Stack,或者cloum+ row 来解决标题栏布局问题。

二、解决方案

方案一:Flex布局(推荐首选)

Flex布局(弹性布局)是鸿蒙中处理线性排列的最佳选择,通过justifyContentalignItems属性可轻松实现组件的均匀分布与居中对齐,尤其适合响应式场景。 利用Flex容器的Row方向(默认水平排列),通过SpaceBetween使三个组件(返回按钮、标题、帮助按钮)在水平方向上两端对齐、中间自适应;alignItems设置为Center确保垂直方向居中。这种布局会自动分配剩余空间,在不同屏幕尺寸下保持组件相对位置稳定。

代码实现

@Entry
@Component
struct FinanceTitleBar {
  // 标题文字可动态修改(适应不同页面)
  @State title: string = "我的资产"

  build() {
    Column() {
      // 标题栏主体
      Flex({ 
        direction: FlexDirection.Row,
        justifyContent: FlexAlign.SpaceBetween,
        alignItems: ItemAlign.Center
      }) {
        // 左侧返回按钮:添加水波纹效果增强交互感
        Button({ type: ButtonType.Circle, stateEffect: true }) {
          Image($r("app.media.ic_back")) // 使用实际返回图标
            .width(24)
            .height(24)
            .fillColor("#333333")
        }
        .width(40)
        .height(40)
        .backgroundColor("transparent")
        .onClick(() => {
          // 金融应用中返回可能需要确认(如表单未保存)
          router.back()
        })

        // 中间标题:金融场景建议加粗突出
        Text(this.title)
          .fontSize(18)
          .fontWeight(FontWeight.Medium)
          .fontColor("#1A1A1A")
          .maxLines(1) // 防止标题过长换行
          .textOverflow({ overflow: TextOverflow.Ellipsis })

        // 右侧帮助按钮:圆形设计符合金融应用的安全感
        Button({ type: ButtonType.Circle, stateEffect: true }) {
          Image($r("app.media.ic_help")) // 使用实际帮助图标
            .width(24)
            .height(24)
            .fillColor("#333333")
        }
        .width(40)
        .height(40)
        .backgroundColor("transparent")
        .onClick(() => {
          // 跳转至帮助中心(金融应用常见需求)
          router.pushUrl({ url: "pages/HelpPage" })
        })
      }
      .width('100%')
      .height(56) // 符合鸿蒙设计规范的标题栏高度
      .padding({ left: 16, right: 16 }) // 左右边距适配触摸区域
      .backgroundColor("#FFFFFF")
      .shadow({ radius: 2, color: "#00000010" }) // 底部阴影增强层次感

      // 页面内容区域(示例)
      Text("资产详情内容...")
        .flexGrow(1)
        .padding(16)
    }
    .width('100%')
    .height('100%')
    .backgroundColor("#F5F5F5")
  }
}

方案二:Stack布局(精准定位场景)

Stack布局(堆叠布局)通过层级叠加组件,适合需要精确定位的场景。核心思路是将标题文字固定在中心,返回按钮与帮助按钮通过绝对定位放置在两侧。

Stack容器默认将子组件堆叠显示,通过alignContent: Alignment.Center使标题文字居中;按钮组件通过position属性设置x轴坐标(左侧按钮距左16vp,右侧按钮距右16vp),y轴坐标设为0实现垂直居中。

代码实现

@Entry
@Component
struct StackTitleBar {
  @State title: string = "理财产品"

  build() {
    Column() {
      Stack({ alignContent: Alignment.Center }) {
        // 中间标题:优先渲染在底层,确保不被按钮遮挡
        Text(this.title)
          .fontSize(18)
          .fontWeight(FontWeight.Medium)
          .fontColor("#1A1A1A")

        // 左侧返回按钮:使用绝对定位
        Button({ type: ButtonType.Circle, stateEffect: true }) {
          Image($r("app.media.ic_back"))
            .width(24)
            .height(24)
        }
        .width(40)
        .height(40)
        .backgroundColor("transparent")
        .position({ x: 16, y: 0 }) // x=16贴左,y=0垂直居中
        .onClick(() => {
          router.back()
        })

        // 右侧帮助按钮:通过计算右间距定位
        Button({ type: ButtonType.Circle, stateEffect: true }) {
          Image($r("app.media.ic_help"))
            .width(24)
            .height(24)
        }
        .width(40)
        .height(40)
        .backgroundColor("transparent")
        .position({ x: "calc(100% - 56vp)", y: 0 }) // 100%宽度减去按钮宽(40)和右距(16)
        .onClick(() => {
          router.pushUrl({ url: "pages/HelpPage" })
        })
      }
      .width('100%')
      .height(56)
      .backgroundColor("#FFFFFF")
      .shadow({ radius: 2, color: "#00000010" })

      // 页面内容
      Text("理财产品列表...")
        .flexGrow(1)
        .padding(16)
    }
    .width('100%')
    .height('100%')
    .backgroundColor("#F5F5F5")
  }
}

方案三:Row+Column组合布局(复杂扩展场景)

当标题栏需要包含更多元素(如用户头像、多按钮组)时,可采用Row嵌套Column的组合布局,通过权重分配实现灵活排列。 外层用Row作为主容器,内部划分三个区域:左侧区域(返回按钮)、中间区域(标题)、右侧区域(帮助按钮+用户头像),每个区域通过flexGrow设置权重(左侧和右侧权重为0,中间权重为1,确保标题占满剩余空间)。

代码实现

@Entry
@Component
struct ComplexTitleBar {
  @State title: string = "账户设置"
  @State userName: string = "张**" // 金融应用隐藏部分姓名

  build() {
    Column() {
      Row() {
        // 左侧区域:仅返回按钮
        Row() {
          Button({ type: ButtonType.Circle, stateEffect: true }) {
            Image($r("app.media.ic_back"))
              .width(24)
              .height(24)
          }
          .width(40)
          .height(40)
          .backgroundColor("transparent")
          .onClick(() => router.back())
        }
        .flexGrow(0) // 不占用额外空间

        // 中间区域:标题(占满剩余空间)
        Row() {
          Text(this.title)
            .fontSize(18)
            .fontWeight(FontWeight.Medium)
        }
        .flexGrow(1) // 权重1,占满剩余空间
        .justifyContent(FlexAlign.Center) // 内部居中

        // 右侧区域:帮助按钮+用户头像
        Row({ space: 12 }) { // 按钮间间距12vp
          Button({ type: ButtonType.Circle, stateEffect: true }) {
            Image($r("app.media.ic_help"))
              .width(24)
              .height(24)
          }
          .width(40)
          .height(40)
          .backgroundColor("transparent")
          .onClick(() => router.pushUrl({ url: "pages/HelpPage" }))

          // 金融应用常见:用户头像
          Image($r("app.media.ic_avatar"))
            .width(36)
            .height(36)
            .borderRadius(18) // 圆形头像
            .onClick(() => router.pushUrl({ url: "pages/UserPage" }))
        }
        .flexGrow(0)
        .alignItems(ItemAlign.Center)
      }
      .width('100%')
      .height(56)
      .padding({ left: 16, right: 16 })
      .backgroundColor("#FFFFFF")
      .shadow({ radius: 2, color: "#00000010" })

      // 页面内容
      Text("账户安全设置...")
        .flexGrow(1)
        .padding(16)
    }
    .width('100%')
    .height('100%')
    .backgroundColor("#F5F5F5")
  }
}

更多关于HarmonyOS鸿蒙Next中常见的标题栏布局方案,请提供源码和步骤讲解的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


鸿蒙Next标题栏常用布局方案如下:

1. 使用系统组件:

import { TitleBar } from '@ohos/common';

TitleBar({
  title: '页面标题',
  menu: [
    { value: '选项1', action: () => {} },
    { value: '选项2', action: () => {} }
  ]
})

2. 自定义布局:

Row() {
  Image($r('app.media.back'))
    .onClick(() => {})
  Text('标题')
    .fontSize(20)
  Blank()
  Image($r('app.media.menu'))
    .onClick(() => {})
}
.height(56)
.width('100%')
.backgroundColor(Color.White)

步骤:

  1. 导入相关组件
  2. 使用Row容器横向排列
  3. 添加返回图标、标题文本、菜单图标
  4. 设置样式和点击事件

在HarmonyOS Next中,实现标题栏布局主要依赖于ArkUI的声明式开发范式。以下提供两种常见方案的核心代码与步骤。

方案一:使用Row容器构建基础标题栏

这是最灵活、最常用的方案,通过Row容器横向排列左侧返回、中间标题、右侧功能图标。

核心代码示例 (ETS):

// TitleBar.ets
@Component
export struct TitleBar {
  @State title: string = '页面标题'

  build() {
    Row({ space: 12 }) {
      // 左侧返回区域
      Image($r('app.media.ic_back'))
        .width(24)
        .height(24)
        .onClick(() => {
          // 处理返回逻辑
        })

      // 中间标题区域(自适应填充)
      Text(this.title)
        .fontSize(20)
        .fontWeight(FontWeight.Medium)
        .layoutWeight(1) // 关键:使用权重填充剩余空间
        .textAlign(TextAlign.Center)

      // 右侧功能区域
      Image($r('app.media.ic_more'))
        .width(24)
        .height(24)
        .onClick(() => {
          // 处理更多操作
        })
    }
    .width('100%')
    .height(56)
    .padding({ left: 16, right: 16 })
    .backgroundColor(Color.White)
    .border({ width: { bottom: 1 }, color: '#F1F3F5' })
  }
}

使用步骤:

  1. 创建自定义组件TitleBar.ets
  2. 在页面中引用:TitleBar({ title: '首页' })
  3. 通过layoutWeight(1)使标题居中自适应
  4. 通过border属性添加底部细分割线

方案二:使用Navigation组件(系统级导航栏)

适用于需要系统统一风格或与页面路由深度集成的场景。

核心代码示例:

// 在Page页面中使用
@Entry
@Component
struct Index {
  @State title: string = '详情页'

  build() {
    Column() {
      Navigation() {
        // 页面内容
        Column() {
          // 主体内容区域
        }
      }
      .title(this.title)
      .titleMode(NavigationTitleMode.Mini) // 标题模式
      .hideBackButton(false) // 显示返回按钮
      .toolBar({
        items: [
          {
            value: '编辑',
            icon: $r('app.media.ic_edit'),
            action: () => { /* 处理编辑操作 */ }
          }
        ]
      })
    }
  }
}

关键布局技巧:

  1. 精确适配:使用vp单位确保不同屏幕密度下的视觉一致性
  2. 安全区域:通过safeArea适配刘海屏和底部手势区域
  3. 状态栏控制:在aboutToAppear中设置状态栏样式
    aboutToAppear() {
      window.setWindowSystemBarProperties({
        statusBarContentColor: '#000000'
      })
    }
    

这两种方案覆盖了大多数应用场景。方案一自定义程度高,适合需要特殊设计的标题栏;方案二与系统集成度好,能自动适配不同设备。开发者可根据具体需求选择。

回到顶部