Skip to content

Mobile Tabs 移动端标签页 ​

提供带下划线的移动端标签栏,可选择只显示标题,或绑定可滑动的内容面板。

何时使用 ​

  • 同一页面有少量平级内容分区时使用 Tabs。
  • 需要底部主导航且带图标时使用 TabBar,不要用 Tabs 代替它。
  • 标签超过一行或内容不是平级分区时,应改用导航列表或筛选器。
  • 需要左右手势切换内容时,在 Tabs 上调用 Swipeable 并提供 Body。

在线体验 ​

真实 gallery 展示顶部 Tabs、多个标题和底部 TabBar 组合,可切换选中项。

Mobile · 导航:NavBar / SearchBar / Tabs / NoticeBar / Grid / TabBar · 在线体验真实 Go 组件
在独立页面打开 ↗

查看此场景的 Go 源码 ↗

基本用法 ​

active 指向当前下标;Header 只构建标签栏,不构建内容面板。

go
import (
    "github.com/tangoui/tango/ui"
    "github.com/tangoui/tango/ui/mobile"
)

var active int

func page(c *ui.Context) {
    mobile.Tabs(c, []string{"推荐", "关注", "热门"}, &active).Header()
    ui.Text(c, "当前:"+[]string{"推荐", "关注", "热门"}[active])
}

常见用法 ​

标签栏和面板 ​

Body 的参数是当前面板下标;按下标构建内容即可。

go
func panes(c *ui.Context) {
    mobile.Tabs(c, []string{"概览", "明细"}, &active).Body(func(i int) {
        if i == 0 {
            ui.Text(c, "概览内容")
            return
        }
        ui.Text(c, "明细内容")
    })
}

手势切换 ​

Swipeable 允许在面板区域横向滑动切换,仍由 active 指针保存选择。

go
func swipeTabs(c *ui.Context) {
    mobile.Tabs(c, []string{"图片", "评论", "详情"}, &active).
        Swipeable().
        Body(func(i int) { ui.Text(c, "面板 "+string(rune('A'+i))) })
}

监听切换 ​

Changed 只在本帧选中项变化时为真,适合更新筛选条件或触发数据加载。

go
func changedTabs(c *ui.Context) {
    tabs := mobile.Tabs(c, []string{"全部", "待处理"}, &active).Header()
    if tabs.Changed() {
        // 根据 active 加载对应数据
    }
}

底部 TabBar ​

主导航使用 TabItem 的稳定 Key 和标题,可选图标与 Badge。

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

var tab = "home"

func bottomNav(c *ui.Context) {
    mobile.TabBar(c, []mobile.TabItem{
        {Key: "home", Title: "首页", Icon: icon.Home},
        {Key: "msg", Title: "消息", Icon: icon.Message, Badge: "5"},
    }, &tab)
}

主题与样式 ​

Tabs 的下划线、激活色和间距读取移动主题;shadcn 风格使用更明显的分段激活状态。底部 TabBar 的图标和 Badge 也跟随 Provider 的令牌。不要用颜色字符串硬编码激活态,必要时读取 mobile.ThemeOf(c)。

注意事项 ​

active 指针必须稳定且范围有效;动态增删标签时同步修正下标。Changed 是逐帧事件,不要把它当作持久选择。标签标题应简短并可被屏幕阅读器理解,面板内容要保留足够点击和滑动空间。RTL 下标签顺序和滑动方向由 ui 方向作用域处理。

示例代码 ​

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

Mobile · 导航:NavBar / SearchBar / Tabs / NoticeBar / Grid / TabBar

gallery 演示 MobileNav 的 Render 函数,源码 page_mobile_gallery.go#L134。

加载中…

Mobile · 展示:Swiper / Skeleton / Empty / Result / Toast

gallery 演示 MobileDisplay 的 Render 函数,源码 page_mobile_gallery.go#L134。

加载中…

API ​

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

入口函数 ​

mobile.Tabs ​

go
func Tabs(c *ui.Context, titles []string, active *int) *TabsW

Tabs adds tabs titled titles; *active is the selected index. Build the panes with Body (or Header for a header only).

In the shadcn style the header is a segmented control: a muted rounded-lg track with the selected title on a raised thumb.

In RTL the first title is at the right, the header scrolls from its right end and the panes follow the titles (the next pane comes in from the left).

ConfigProvider defaults ("mobile.Tabs"): "swipeable" (bool).

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

mobile.Swiper ​

go
func Swiper(c *ui.Context, cur *int) *SwiperW

Swiper creates a carousel; *cur is the current slide (nil keeps it internally). Set options, then build slides with Slides. In RTL the slides run right to left: the next slide comes in from the left, a swipe to the right shows it and the indicator starts at the right.

ConfigProvider defaults ("mobile.Swiper"): "autoplay" (time.Duration or ms), "loop", "indicator" (bool), "height", "radius" (numbers), "indicatorColor" ("white" for IndicatorWhite).

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

TabsW 方法 ​

mobile.TabsW:TabsW is a tab header with an underline and (optionally swipeable) content panes.

方法说明签名
BodyBody builds the header and the panes; pane(i) builds pane i.Body(pane func(i int)) *TabsW
ChangedChanged reports a tab switch this frame.Changed() bool
HeaderHeader builds the header only.Header() *TabsW
SwipeableSwipeable lets a horizontal swipe on the content switch tabs.Swipeable() *TabsW

SwiperW 方法 ​

mobile.SwiperW:SwiperW is a swipeable carousel of slides with indicator dots.

方法说明签名
AutoplayAutoplay advances every d.Autoplay(d time.Duration) *SwiperW
ChangedChanged reports a slide change this frame.Changed() bool
HeightHeight sets the slide height (default 150).Height(px float32) *SwiperW
IndicatorIndicator toggles the dots.Indicator(on bool) *SwiperW
IndicatorColorIndicatorColor sets the color of the current slide's dot and of the others (a zero inactive color is active at 40% opacity).IndicatorColor(active, inactive theme.Color) *SwiperW
IndicatorWhiteIndicatorWhite draws white dots (Ant Design Mobile's indicator color "white"), for slides that are images or saturated colors.IndicatorWhite() *SwiperW
LoopLoop wraps from the last slide to the first.Loop() *SwiperW
RadiusRadius rounds the corners.Radius(px float32) *SwiperW
SlidesSlides builds n slides; slide(i) builds slide i.Slides(n int, slide func(i int)) *SwiperW

相关链接 ​

以 MIT 许可发布