Skip to content

Avatar 头像 ​

用头像、首字母或图标表达用户、团队和对象身份。

何时使用 ​

  • 用户列表、评论、协作成员和账户入口中展示身份。
  • 需要叠放多个成员时使用 AvatarGroup。
  • 图片需要预览、缩放时改用 Image 图片;只是状态提示时用 Badge 徽章。

在线体验 ​

Badge 徽标 / Avatar 头像 · 在线体验真实 Go 组件
在独立页面打开 ↗

查看此场景的 Go 源码 ↗

基本用法 ​

go
import "github.com/tangoui/tango/ui"

func page(c *ui.Context) {
    ui.Avatar(c, "张三").Size(40).A11yLabel("张三")
}

头像会从 label 生成缩写。可用 Icon 显示图标,或用 Image 显示图片。两者都设置时优先显示图片。

常见用法 ​

方形与自定义颜色 ​

go
func avatarVariants(c *ui.Context) {
    ui.Avatar(c, "AB").Square().Size(32)
    ui.Avatar(c, "A").Size(40)
}

图片头像 ​

go
func imageAvatar(c *ui.Context, img image.Image) {
    ui.Avatar(c, "").Image(img).Size(40).A11yLabel("项目头像")
}

使用时导入标准库 image;图片加载和缓存由应用负责。

头像组 ​

go
func members(c *ui.Context) {
    ui.AvatarGroup(c).Max(3).Size(32).Body(func() {
        ui.Avatar(c, "李"); ui.Avatar(c, "王"); ui.Avatar(c, "赵"); ui.Avatar(c, "孙")
    })
}

主题与样式 ​

三套主题分别通过头像背景、文字和组重叠令牌适配 Ant Design、Element Plus、shadcn。ConfigProvider.Defaults("Avatar", ...) 支持 size、shape 和 gap,ConfigProvider.Defaults("AvatarGroup", ...) 支持 maxCount。

注意事项 ​

状态放在页面变量或后端数据中;头像本身是展示控件。图片头像应提供 A11yLabel,不要只依赖首字母。RTL 下组的重叠方向自动镜像。

示例代码 ​

以下是上方在线演示运行的 Go 源码(来自官方 gallery),点击标题展开。函数接收的 c *ui.Context 由窗口的 UI 循环每帧传入,接入方式见 Go GUI 入门。

Badge 徽标 / Avatar 头像

gallery 演示 Badge 的 Render 函数,源码 page_data_misc.go#L141。

加载中…

Badge 负数、隐藏与标题

gallery 演示 BadgeAlign 的 Render 函数,源码 page_b2g.go#L65。

加载中…

API ​

签名与说明由 site/tools/compdoc 从 Go 源码和文档注释提取;点击名称跳到 Go API 参考 的对应条目。

入口函数 ​

ui.Avatar ​

go
func Avatar(c *Context, label string) *AvatarW

Avatar shows text initials (or an icon when label is "").

ConfigProvider.Defaults("Avatar", ...) keys: "size" (float32 px or ControlSize), "shape" ("circle" | "square"), "gap" (px, default 4).

返回 *AvatarW,可链式调用的方法见 AvatarW 方法。

ui.Gap ​

go
func Gap(c *Context, size float32)

Gap inserts a fixed-size blank.

ui.AvatarGroup ​

go
func AvatarGroup(c *Context) *AvatarGroupW

AvatarGroup starts a group; call Body with the avatars.

ConfigProvider.Defaults("AvatarGroup", ...) keys: "maxCount" (int).

返回 *AvatarGroupW,可链式调用的方法见 AvatarGroupW 方法。

AvatarW 方法 ​

ui.AvatarW:AvatarW is a round user avatar.

方法说明签名
A11yLabelA11yLabel sets the accessible name (the person's name; the default is the avatar text, or "avatar" for an icon or image avatar).A11yLabel(s string) *AvatarW
BgBg(col theme.Color) *AvatarW
GapGap sets the space kept on each side of the text; longer text is scaled down to fit (antd gap, default 4).Gap(px float32) *AvatarW
IconIcon shows an icon instead of the text (icon avatar).Icon(ic *icon.Icon) *AvatarW
ImageImage(img image.Image) *AvatarW
LargeLarge uses the large container size (antd 40px, Element 56px).Large() *AvatarW
ShapeShape sets "circle" (default) or "square".Shape(s string) *AvatarW
SizeSize(px float32) *AvatarW
SmallSmall uses the small container size (antd size="small": 24px).Small() *AvatarW
SquareSquare() *AvatarW

AvatarGroupW 方法 ​

ui.AvatarGroupW:AvatarGroupW stacks avatars with overlap.

方法说明签名
BodyBody builds the avatars.Body(fn func()) *Box
MaxMax folds avatars beyond n into a "+k" avatar; hovering it shows the folded avatars in a popover (antd max.count / max.popover).Max(n int) *AvatarGroupW
MaxPopoverMaxPopover shows (default) or hides the popover listing the folded avatars; placement and trigger as in Popover (default "top", "hover").MaxPopover(on bool, placement, trigger string) *AvatarGroupW
MoreBgMoreBg sets the background of the "+k" avatar (antd max.style).MoreBg(col theme.Color) *AvatarGroupW
SizeSize sets the size of the "+k" avatar (match your avatars; default 32).Size(px float32) *AvatarGroupW

相关链接 ​

以 MIT 许可发布