向卡片中添加文字和图片
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
本页介绍了如何向卡片添加文本和图片并设置其格式。
如需详细了解如何构建卡片,请参阅为 Google Chat 应用构建卡片。
使用卡片构建器为 Chat 应用设计消息和界面并预览:
打开卡片构建器
前提条件
已配置为接收和响应互动事件的 Google Chat 应用。如需创建交互式 Chat 应用,请根据要使用的应用架构完成以下任一快速入门 Codelab:
添加图片或图标
本部分介绍了如何向卡片添加图片、图片组件和图标等视觉元素。
添加图片
Image
widget 显示托管在 HTTPS 网址上的 PNG 或 JPG 图片。
显示图片的宽度会占满显示卡片的整个宽度,而其高度会进行调整以保持图片的宽高比。Image
widget 支持用户点击图片时发生的 onclick
操作。onclick
操作的示例包括:
- 打开带有
OpenLink
的超链接,例如指向 Google Chat 开发者文档的超链接 https://developers.google.com/chat
。
- 运行可执行自定义函数(例如调用 API)的操作。
Image
widget 具有以下限制:
- 仅支持 PNG 和 JPG 格式的图片。
- 主机网址必须为
HTTPS
。
- 为确保卡片效果良好,建议图片大小上限为 2MB。
以下是包含 Image
widget 的卡片。它显示了 Google Chat 开发者文档着陆页图片。当用户点击该图片时,Google Chat 开发者文档会在新标签页中打开。
添加图片组件
Image
widget 是一种样式有限的图片。借助 imageCompent
widget,您可以对图片应用 cropStyle
和 borderStyle
。
以下示例展示了网格中的两张图片,其中一张图片经过裁剪:
您可以通过应用 cropStyle
来调整图片组件的形状。您可以将多种形状应用于图片:
- 使用
SQUARE
应用方形裁剪。
- 使用
CIRCLE
应用圆形裁剪。
- 使用
RECTANGLE_CUSTOM
应用具有自定义宽高比的矩形裁剪。例如,您可以使用 RECTANGLE_4_3
应用宽高比为 4:3 的矩形剪裁。
以下示例展示了一个网格中的五张图片,每张图片都应用了不同的 cropStyle
:
添加图标
Icon
widget 表示内置图标或自定义图标。您可以向卡片添加图标,以执行以下任一操作:
以下是一个包含内置图标的 Icon
组件的卡片:
下表列出了可用于卡片消息的内置图标:
flight |
AIRPLANE |
bookmark |
BOOKMARK |
directions_bus |
BUS |
directions_car |
CAR |
schedule |
时钟 |
confirmation_number |
CONFIRMATION_NUMBER_ICON |
subject |
说明 |
attach_money |
DOLLAR |
mail |
电子邮件 |
event_seat |
EVENT_SEAT |
flight_land |
FLIGHT_ARRIVAL |
flight_takeoff |
FLIGHT_DEPARTURE |
hotel |
HOTEL |
grade |
HOTEL_ROOM_TYPE |
insert_invitation |
邀请 |
location_on |
MAP_PIN |
card_membership |
会员 |
people |
MULTIPLE_PEOPLE |
person |
PERSON |
local_phone |
PHONE |
restaurant |
RESTAURANT_ICON |
shopping_cart |
SHOPPING_CART |
grade |
STAR |
store |
STORE |
local_play |
TICKET |
train |
TRAIN |
videocam |
VIDEO_CAMERA |
play_circle_filled |
VIDEO_PLAY |
向卡片添加文字
本部分介绍了如何在卡片中添加和设置文本格式。
添加一段格式化文本
TextParagraph
widget 用于显示一段文本,并可选择性地添加 HTML 格式。设置这些 widget 的文本内容时,只需添加相应的 HTML 标记即可。
如需详细了解支持哪些 HTML 代码,请参阅设置卡片中显示的文本的格式。
例如,段落文本可使用以下格式:
- 使用 HTML
<b>
、<u>
、<i>
标记显示粗体、下划线或斜体文本。
- 使用 HTML
<a href="https://www.google.com">hyperlinks</a>
链接到网站。
- 使用 HTML
<font color="#ea9999">font tags</font>
添加一些颜色。
每个 TextParagraph
widget 都会呈现为新段落,可以将其视为类似于 HTML <p>
标记。
以下卡片包含两个 TextParagraph
widget,用于显示两个具有简单 HTML 格式的段落:
添加可收起的文本段落
用户可以根据需要展开折叠式文本段落,查看更多信息。
此 widget 非常适合呈现长篇内容或附加详情,用户可以在选择后探索这些内容,从而打造动态且互动的用户体验。
显示带有装饰元素的文字
DecoratedText
widget 用于显示具有可选布局和功能的文本。例如:
- 使用
startIcon
在文字前面显示 icon
。
- 使用
topLabel
在文字前显示标题。
- 添加带有
button
的可点击按钮或带有 switchControl
的可切换开关。
当您需要以易于理解的互动方式呈现信息时,请使用 DecoratedText
widget。此 widget 非常适合用于联系人卡片、订单状态更新和工单通知等使用情形。
DecoratedText
widget 支持简单的文本 HTML 格式。设置这些 widget 的文本内容时,只需添加相应的 HTML 标记即可。如需详细了解支持哪些 HTML 标记,请参阅卡片文本格式。
以下卡片包含一个 DecoratedText
widget,用于显示电子邮件地址、在线状态、电话号码和网站等联系信息:
问题排查
当 Google Chat 应用或卡片返回错误时,Chat 界面会显示一条消息,提示“出了点问题”。
或“无法处理您的请求”。有时,Chat 界面不会显示任何错误消息,但 Chat 应用或卡片会产生意外结果;例如,卡片消息可能不会显示。
虽然聊天界面中可能不会显示错误消息,但如果为聊天应用启用错误日志记录,系统会提供描述性错误消息和日志数据,帮助您修复错误。如需有关查看、调试和修复错误的帮助,请参阅排查和修复 Google Chat 错误。
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-29。
[null,null,["最后更新时间 (UTC):2025-08-29。"],[[["\u003cp\u003eThis page guides you on adding and formatting text and images within Google Chat cards to enhance their visual appeal and interactivity.\u003c/p\u003e\n"],["\u003cp\u003eYou can incorporate images, icons, and formatted text paragraphs using various widgets and HTML formatting options.\u003c/p\u003e\n"],["\u003cp\u003eLearn how to use \u003ccode\u003eImage\u003c/code\u003e, \u003ccode\u003eimageComponent\u003c/code\u003e, \u003ccode\u003eIcon\u003c/code\u003e, \u003ccode\u003eTextParagraph\u003c/code\u003e, and \u003ccode\u003eDecoratedText\u003c/code\u003e widgets to build engaging cards.\u003c/p\u003e\n"],["\u003cp\u003eLeverage built-in or custom icons, apply cropping and border styles to images, and add interactive elements like buttons or hyperlinks.\u003c/p\u003e\n"],["\u003cp\u003eRefer to the troubleshooting section for assistance if you encounter any errors while implementing these features.\u003c/p\u003e\n"]]],[],null,["# Add text and images to cards\n\nThis page explains how to add and format text and images to cards.\n\nTo learn more about building cards, see\n[Build cards for Google Chat apps](/workspace/chat/design-components-card-dialog).\n\n\n*** ** * ** ***\n\nUse the Card Builder to design and preview messaging and user interfaces for Chat apps:\n[Open the Card Builder](https://addons.gsuite.google.com/uikit/builder)\n\n*** ** * ** ***\n\n\u003cbr /\u003e\n\nPrerequisites\n-------------\n\n\nA Google Chat app that's configured to receive and respond to [interaction events](/workspace/chat/receive-respond-interactions). To create an\ninteractive Chat app, complete one of the following quickstarts based\non the [app architecture](/workspace/chat/structure) that you want to use:\n\n- [HTTP service](/workspace/chat/quickstart/gcf-app) with Google Cloud Functions\n- [Google Apps Script](/workspace/chat/quickstart/apps-script-app)\n- [Google Cloud Dialogflow CX](/workspace/chat/build-dialogflow-chat-app-natural-language)\n- [Google Cloud Pub/Sub](/workspace/chat/quickstart/pub-sub)\n\nAdd images or icons\n-------------------\n\nThis section explains how to add visual elements to cards such as images,\nimage components, and icons.\n\n### Add an image\n\n[`Image` widget](/workspace/chat/api/reference/rest/v1/cards#image)\ndisplays a PNG or JPG image hosted on an HTTPS URL.\nThe displayed image's width fills the entire width of the displayed card, and\nits height adjusts to maintain the image's aspect ratio. The `Image` widget\nsupports\n[`onclick` actions](/workspace/chat/api/reference/rest/v1/cards#OnClick_1)\nthat occur when users click the image. Example of `onclick` actions include:\n\n- Open a hyperlink with [`OpenLink`](/workspace/chat/api/reference/rest/v1/cards#OpenLink_1), such as a hyperlink to the Google Chat developer documentation, `https://developers.google.com/chat`.\n- Run an [action](/workspace/chat/api/reference/rest/v1/cards#action) that runs a custom function, like calling an API.\n\nThe `Image` widget has the following limitations:\n\n- Only PNG and JPG images are supported.\n- The host URL must be `HTTPS`.\n- To ensure performant cards, maximum recommended image size is 2MB.\n\nThe following is a card consisting of an `Image` widget. It displays the\nGoogle Chat developer documentation landing page image. When users click the\nimage, the Google Chat developer documentation opens in a new tab.\n\n### Add an image component\n\nThe `Image` widget is an image with limited styling. An\n[`imageCompent` widget](/workspace/chat/api/reference/rest/v1/cards#imagecomponent)\nlets you apply `cropStyle` and `borderStyle` to an image.\n\nThe following example shows two images in a grid where one of the images is\ncropped:\n\nYou can adjust the shape of an image component by applying a\n[`cropStyle`](/workspace/chat/api/reference/rest/v1/cards#imagecropstyle).\nThere are several shapes to apply to an image:\n\n- Use `SQUARE` to apply a square crop.\n- Use `CIRCLE` to apply a circular crop.\n- Use `RECTANGLE_CUSTOM` to apply a rectangular crop with a custom aspect ratio. For example, you can use `RECTANGLE_4_3` to apply a rectangular crop with a 4:3 aspect ratio.\n\nThe following example shows five images in a grid with a different `cropStyle`\napplied to each image:\n\n### Add an icon\n\nThe\n[`Icon` widget](/workspace/chat/api/reference/rest/v1/cards#textparagraph)\nrepresents either a\n[built-in](/workspace/chat/format-messages#builtinicons)\nicon or\n[custom](/workspace/chat/format-messages#customicons)\nicon. You add icons to cards to do any of the following:\n\n- Display a standalone icon.\n- Display an icon in front of related text, as part of a [`DecoratedText`](/workspace/chat/decorated-text) widget.\n- Display an icon as an interactive button, as part of a [`ButtonList`](/workspace/chat/button-list) widget.\n\nThe following is a card consisting of an `Icon` component with a built-in icon:\n\nThe following table lists the built-in icons that are available for card messages:\n\n|-------------------|---------------------|---------------------|------------------------------|\n| flight | **AIRPLANE** | bookmark | **BOOKMARK** |\n| directions_bus | **BUS** | directions_car | **CAR** |\n| schedule | **CLOCK** | confirmation_number | **CONFIRMATION_NUMBER_ICON** |\n| subject | **DESCRIPTION** | attach_money | **DOLLAR** |\n| mail | **EMAIL** | event_seat | **EVENT_SEAT** |\n| flight_land | **FLIGHT_ARRIVAL** | flight_takeoff | **FLIGHT_DEPARTURE** |\n| hotel | **HOTEL** | grade | **HOTEL_ROOM_TYPE** |\n| insert_invitation | **INVITE** | location_on | **MAP_PIN** |\n| card_membership | **MEMBERSHIP** | people | **MULTIPLE_PEOPLE** |\n| person | **PERSON** | local_phone | **PHONE** |\n| restaurant | **RESTAURANT_ICON** | shopping_cart | **SHOPPING_CART** |\n| grade | **STAR** | store | **STORE** |\n| local_play | **TICKET** | train | **TRAIN** |\n| videocam | **VIDEO_CAMERA** | play_circle_filled | **VIDEO_PLAY** |\n\nAdd text to a card\n------------------\n\nThis section explains how to add and format text in a card.\n\n### Add a paragraph of formatted text\n\nThe\n[`TextParagraph` widget](/workspace/chat/api/reference/rest/v1/cards#textparagraph)\ndisplays a paragraph of text with optional HTML formatting. When setting\nthe text content of these widgets, just include the corresponding HTML tags.\nFor more information about which HTML tags are supported, see\n[Format text that appears in cards](/workspace/chat/format-messages#card-formatting).\n\nFor example, the following formatting is available for paragraph text:\n\n- Display bold, underlined, or italicized text with HTML `\u003cb\u003e`, `\u003cu\u003e`, `\u003ci\u003e` tags.\n- Link to websites with HTML `\u003ca href=\"https://www.google.com\"\u003ehyperlinks\u003c/a\u003e`.\n- Add some color with HTML `\u003cfont color=\"#ea9999\"\u003efont tags\u003c/font\u003e`.\n\nEach `TextParagraph` widget renders as a new paragraph, and can be thought of\nas similar to an HTML `\u003cp\u003e` tag.\n\nThe following is a card consisting of two `TextParagraph` widgets used to\ndisplay two paragraphs with simple HTML formatting:\n\n#### Add a collapsible text paragraph\n\nThe collapsible text paragraphs let users reveal more information on demand.\nThis widget is perfect for presenting lengthy content or additional details that\ncan be explored when selected, creating a dynamic and interactive user\nexperience.\n\n### Display text with decorative elements\n\nThe\n[`DecoratedText` widget](/workspace/chat/api/reference/rest/v1/cards#DecoratedText)\ndisplays text with optional layout and capabilities. For example:\n\n- Display an `icon` in front of the text with `startIcon`.\n- Show a title before the text with `topLabel`.\n- Add a clickable button with `button` or switchable toggle with `switchControl`.\n\nUse the `DecoratedText` widget when you need to present information in an\neasy-to-consume and interactive way. The widget is perfect for use cases such as\ncontact cards, order status updates, and work ticket notifications.\n\nThe `DecoratedText` widget supports simple text HTML formatting. When setting\nthe text content of these widgets, just include the corresponding HTML tags. For\nmore information about which HTML tags are supported, see\n[Card text formatting](/workspace/chat/format-messages#view_text_formatting_sent_in_a_message).\n\nThe following is a card consisting of a `DecoratedText` widget used to display\ncontact details, such as email address, online status, phone number, and\nwebsite:\n\nTroubleshoot\n------------\n\n\nWhen a Google Chat app or\n[card](/workspace/chat/create-messages#create) returns an error, the\nChat interface surfaces a message saying \"Something went wrong.\"\nor \"Unable to process your request.\" Sometimes the Chat UI\ndoesn't display any error message, but the Chat app or\ncard produces an unexpected result; for example, a card message might not\nappear.\n\n\nAlthough an error message might not display in the Chat UI,\ndescriptive error messages and log data are available to help you fix errors\nwhen error logging for Chat apps is turned on. For help viewing,\ndebugging, and fixing errors, see\n[Troubleshoot and fix Google Chat errors](/workspace/chat/troubleshoot).\n\nRelated topics\n--------------\n\n- [View Chat app samples](/workspace/chat/samples) that use cards.\n- [`Image`](/workspace/chat/api/reference/rest/v1/cards#image)\n- [`imageCompent`](/workspace/chat/api/reference/rest/v1/cards#imagecomponent)\n- [`cropStyle`](/workspace/chat/api/reference/rest/v1/cards#imagecropstyle)\n- [`Icon`](/workspace/chat/api/reference/rest/v1/cards#textparagraph)\n- [`TextParagraph`](/workspace/chat/api/reference/rest/v1/cards#textparagraph)\n- [`DecoratedText`](/workspace/chat/api/reference/rest/v1/cards#DecoratedText)"]]