HarmonyOS Next 办公应用:消息气泡组件的实现(一)

HarmonyOS Next 办公应用:消息气泡组件的实现

概述

在 HarmonyOS Next 办公类应用开发中,实现消息气泡组件是常见的交互需求。消息气泡可用于展示聊天消息、通知等内容,并且根据消息类型和发送方向有不同的显示样式。下面将介绍如何构建一个支持多种消息类型的消息气泡组件。

核心代码功能及对应代码段

1. 组件状态与属性定义

MessageBubble 组件定义了多个状态和属性,用于控制消息气泡的显示样式和内容。

typescript 复制代码
@Component
export struct MessageBubble {
  receivedName: string ='';
  @Link currentFeatureIndex: number;
  private isReceived: boolean = false;
  private content: string = '';
  private isAppletMsg?: boolean;
  private isDocumentMsg?: boolean;
  avatar1: Resource = $r('app.media.avatar1');
  avatar2: Resource = $r('app.media.avatar7');
  // ...
}
  • receivedName:接收方的名称。
  • isReceived:标记消息是否为接收的消息。
  • content:消息的具体内容。
  • isAppletMsg 和 isDocumentMsg:标记消息是否为小程序消息或文档消息。
  • avatar1 和 avatar2:分别表示接收方和发送方的头像资源。
2. 消息气泡布局构建

build 方法构建了消息气泡的整体布局,根据 isReceived 的值决定消息气泡的对齐方式和头像显示位置。

typescript 复制代码
build() {
  Column() {
    Flex({ justifyContent: this.isReceived ? FlexAlign.Start : FlexAlign.End, direction: FlexDirection.Row }) {
      if (this.isReceived) {
        Image(conversationListData.find((item) => item.name === this.receivedName)?.icon || this.avatar1)
          .width('40vp')
          .height('40vp')
          .flexShrink(0)
      }
      Column() {
        Stack({ alignContent: this.isReceived ? Alignment.TopStart : Alignment.TopEnd }) {
          // 路径绘制,用于消息气泡的三角箭头
          Path()
            .commands('M-10 1 L0 18 L32 1 Z')
            .fillOpacity(1)
            .stroke(Color.White)
            .strokeWidth(1)
            .fill(Color.White)
            .visibility(this.isReceived ? Visibility.Visible : Visibility.None)
          Path()
            .commands('M23 1 L0 28 L-10 1 Z')
            .fillOpacity(1)
            .stroke(Color.White)
            .strokeWidth(1)
            .fill(Color.White)
            .visibility(this.isReceived ? Visibility.None : Visibility.Visible)
            .zIndex(2)
            .margin({ right: '-10vp' })
          Column() {
            this.MessageContent()
          }
          .padding({
            left: '12vp',
            right: '12vp',
            top: '10vp',
            bottom: '10vp'
          })
          .backgroundColor(Color.White)
          .borderRadius(8)
        }
        .padding({
          top: '1vp',
          left: '20vp',
          right: '20vp',
          bottom: '22vp'
        })
        .width('100%')
      }
      .width('100%')

      if (!this.isReceived) {
        Image(this.avatar2)
          .width('40vp')
          .height('40vp')
          .flexShrink(0)
      }
    }
  }
  .margin({ left: '15vp', right: '16vp' })
}
  • 通过 Flex 组件根据 isReceived 控制消息气泡的对齐方式。
  • 使用 Path 组件绘制消息气泡的三角箭头,根据消息方向显示不同的箭头。
  • 调用 MessageContent 方法显示消息内容。
相关推荐
逆回7 小时前
给鸿蒙手机做一只 3D 宠物:五个把我卡住的问题
harmonyos
钉先生贾9 小时前
性能、体积与稳定性:怎样写出可信的适配结论
华为·harmonyos
笑 总不会犯错14 小时前
摆脱多端数据撕裂,鸿蒙分布式数据库同步与冲突合并
harmonyos
用户1179104883314 小时前
鸿蒙签名失败不是玄学:5 个证据点把 hapsigntool 一次排通 GitHub:swsgbl/hmharness
开源·ai编程·harmonyos
redtank200017 小时前
纯血鸿蒙小工具三件套实测:弹幕手持、爱记录与仓库管理软件
学习·华为·智能手机·harmonyos
用户86194177207411 天前
uni-app 的蓝牙 API 在鸿蒙上不生效?我写了个兼容层,让老代码零改动跑起来
harmonyos
HarmonyOS_SDK1 天前
【HarmonyOS SDK Skills】挂载 Agent Skill,轻松跑通鸿蒙应用广告变现
harmonyos
闫东阳LordGalaxy1 天前
给小艺装上数学能力:数易 App 接入智能体 FunctionComponent 与 A2A 双通路实战
harmonyos
Dream-Y.ocean1 天前
鸿蒙PC平台逻控 PLC 编辑器适配实战:基于 Electron 壳方案的纯 JavaScript 梯形图编程与仿真引擎开发
javascript·编辑器·harmonyos
OH_TPC2 天前
HarmonyOS APP开发---"好物清单"购物清单App,需要用到这个库
harmonyos