Ionic Tab 选项卡
系统梳理 Ionic 1 Tab 选项卡容器(.tabs/.tab-item)的默认文字形态、十种配色与隐藏类、纯图标/顶图标/左图标三种图标排布,以及 Android 风格条纹选项卡(tabs-striped/tabs-top)与顶部配合要点。
学完这一章,你能够
- 系统梳理 Ionic 1 Tab 选项卡容器(.tabs/.tab-item)的默认文字形态、十种配色与隐藏类、纯图标/顶图标/左图标三种图标排布,以及 Android 风格条纹选项卡(tabs-striped/tabs-top)与顶部配合要点。
基础文字选项卡、十色主题与隐藏控制
Ionic Tab(选项卡)是由一组水平均分的按钮或链接构成的移动端主流导航栏,支持纯文本、纯图标或图文混排。基础结构使用 `.tabs` 作为外层栏容器、每个切换项使用 `.tab-item`(默认只显示文字)。配色方面,除默认风格 `tabs-default` 外,支持全部九种主题类:`tabs-light`、`tabs-stable`、`tabs-positive`、`tabs-calm`、`tabs-balanced`、`tabs-energized`、`tabs-assertive`、`tabs-royal` 与 `tabs-dark`;当需要在特定子页面临时隐藏选项卡栏时,可为容器添加 `tabs-item-hide` 类。
图标选项卡的三种排布方式
借助不同的图标布局修饰类,可以灵活调整选项卡内的图文关系:`tabs-icon-only` 仅展示居中图标(如 `ion-home`、`ion-star`、`ion-gear-a`),适合直观的通用功能栏;`tabs-icon-top` 将图标置于文字正上方,是 iOS 底部标签栏的经典样式;`tabs-icon-left` 将图标与文字横向并排放置,更节省垂直高度,常见于紧凑导航条或卡片底部操作区。
Android 条纹选项卡与现代 ion-tabs 演进
若需要贴合 Android Material 风格的顶部指示条纹外观,可在外层包裹容器添加 `tabs-striped` 类,并可叠加 `tabs-top` 将选项卡固定至页面顶部。条纹选项卡的背景与高亮文字/指示条颜色分别由 `tabs-background-{color}` 与 `tabs-color-{color}` 独立控制(`{color}` 取值覆盖 `default` 与全部九套主题色),当前激活项通过 `.tab-item.active` 标识。需特别留意:当页面顶部同时存在 Header 标题栏与顶部选项卡时,必须在内容区配合 `has-tabs-top` 类避免内容被遮挡。源页面的 5 个示例按钮与 1 个演示 `iframe` 仅做静态登记;现代 Ionic 7/8 对应 `<ion-tabs>`、`<ion-tab-bar slot="bottom|top">` 与 `<ion-tab-button>`。
运行 Ionic 1 文字/图标/条纹选项卡布局与切换状态机离线核验
"""Offline Ionic 1 Tabs (text, icon-only, icon-top, icon-left, striped, color/hide) verifier for Ch14 (tab)."""
from __future__ import annotations
import json
from lxml import html
VERSION = "CPython 3.12.14 / lxml offline Ionic UI & component verifier"
TAB_COLORS = ["default", "light", "stable", "positive", "calm", "balanced", "energized", "assertive", "royal", "dark"]
def make_case(name: str, summary: str, expected, actual) -> dict:
return {
"name": name,
"summary": summary,
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_text_tabs_and_palette() -> dict:
markup = """
<div class="tabs">
<a class="tab-item">主页</a>
<a class="tab-item">收藏</a>
<a class="tab-item">设置</a>
</div>
"""
root = html.fragment_fromstring(markup)
labels = [a.text_content().strip() for a in root.xpath("./a[@class='tab-item']")]
has_icons = bool(root.xpath(".//i"))
actual = {
"container_class": root.get("class"),
"tab_labels": labels,
"has_icons": has_icons,
"color_class_count": len(TAB_COLORS),
"hide_class": "tabs-item-hide",
}
expected = {
"container_class": "tabs",
"tab_labels": ["主页", "收藏", "设置"],
"has_icons": False,
"color_class_count": 10,
"hide_class": "tabs-item-hide",
}
return make_case(
"tabs_basic_text_and_color_classes",
"Verify default text-only .tabs/.tab-item markup, 10 tabs-{color} classes, and tabs-item-hide",
expected,
actual,
)
def case_icon_layout_modes() -> dict:
markup = """
<section>
<div class="tabs tabs-icon-only">
<a class="tab-item"><i class="icon ion-home"></i></a>
<a class="tab-item"><i class="icon ion-star"></i></a>
<a class="tab-item"><i class="icon ion-gear-a"></i></a>
</div>
<div class="tabs tabs-icon-top">
<a class="tab-item" href="#"><i class="icon ion-home"></i>主页</a>
<a class="tab-item" href="#"><i class="icon ion-star"></i>收藏</a>
<a class="tab-item" href="#"><i class="icon ion-gear-a"></i>设置</a>
</div>
<div class="tabs tabs-icon-left">
<a class="tab-item"><i class="icon ion-home"></i>主页</a>
<a class="tab-item"><i class="icon ion-star"></i>收藏</a>
<a class="tab-item"><i class="icon ion-gear-a"></i>设置</a>
</div>
</section>
"""
root = html.fragment_fromstring(markup)
modes = []
for div in root.xpath("./div"):
mode_cls = [c for c in div.get("class", "").split() if c.startswith("tabs-icon-")][0]
texts = [a.text_content().strip() for a in div.xpath("./a")]
icon_count = len(div.xpath("./a/i[contains(@class,'icon')]"))
modes.append({
"mode": mode_cls,
"icon_count": icon_count,
"has_visible_text": any(bool(t) for t in texts),
})
actual = modes
expected = [
{"mode": "tabs-icon-only", "icon_count": 3, "has_visible_text": False},
{"mode": "tabs-icon-top", "icon_count": 3, "has_visible_text": True},
{"mode": "tabs-icon-left", "icon_count": 3, "has_visible_text": True},
]
return make_case(
"tabs_icon_only_top_and_left",
"Verify tabs-icon-only, tabs-icon-top, and tabs-icon-left container layouts",
expected,
actual,
)
def case_striped_android_tabs() -> dict:
markup = """
<section>
<div class="tabs-striped tabs-top tabs-background-positive tabs-color-light">
<div class="tabs">
<a class="tab-item active" href="#"><i class="icon ion-home"></i>Test</a>
<a class="tab-item" href="#"><i class="icon ion-star"></i>Favorites</a>
<a class="tab-item" href="#"><i class="icon ion-gear-a"></i>Settings</a>
</div>
</div>
<div class="tabs-striped tabs-color-assertive">
<div class="tabs">
<a class="tab-item active" href="#"><i class="icon ion-home"></i>Test</a>
<a class="tab-item" href="#"><i class="icon ion-star"></i>Favorites</a>
<a class="tab-item" href="#"><i class="icon ion-gear-a"></i>Settings</a>
</div>
</div>
</section>
"""
root = html.fragment_fromstring(markup)
wrappers = root.xpath("./div")
w1_cls = set(wrappers[0].get("class", "").split())
w2_cls = set(wrappers[1].get("class", "").split())
active_labels = [a.text_content().strip() for a in root.xpath(".//a[contains(@class,'active')]")]
actual = {
"top_striped_classes_ok": {"tabs-striped", "tabs-top", "tabs-background-positive", "tabs-color-light"} <= w1_cls,
"bottom_striped_classes_ok": {"tabs-striped", "tabs-color-assertive"} <= w2_cls,
"active_tab_labels": active_labels,
"header_companion_class": "has-tabs-top",
}
expected = {
"top_striped_classes_ok": True,
"bottom_striped_classes_ok": True,
"active_tab_labels": ["Test", "Test"],
"header_companion_class": "has-tabs-top",
}
return make_case(
"tabs_striped_top_background_and_color",
"Verify Android-style tabs-striped, tabs-top, tabs-background-*, tabs-color-*, active item, and has-tabs-top",
expected,
actual,
)
def case_tab_navigation_state_machine() -> dict:
tabs = ["Test", "Favorites", "Settings"]
active = "Test"
history = [active]
for target in ("Favorites", "Settings", "Test"):
assert target in tabs
active = target
history.append(active)
actual = {
"final_active": active,
"transition_history": history,
"modern_component": "<ion-tabs><ion-tab-bar slot=\"bottom\"><ion-tab-button tab=\"home\"></ion-tab-button></ion-tab-bar></ion-tabs>",
}
expected = {
"final_active": "Test",
"transition_history": ["Test", "Favorites", "Settings", "Test"],
"modern_component": "<ion-tabs><ion-tab-bar slot=\"bottom\"><ion-tab-button tab=\"home\"></ion-tab-button></ion-tab-bar></ion-tabs>",
}
return make_case(
"tabs_active_switching_and_modern_mapping",
"Verify single-active tab navigation state transitions and Ionic 7/8 <ion-tabs> mapping",
expected,
actual,
)
def main() -> None:
cases = [
case_text_tabs_and_palette(),
case_icon_layout_modes(),
case_striped_android_tabs(),
case_tab_navigation_state_machine(),
]
print(json.dumps({"version": VERSION, "cases": cases}, ensure_ascii=False, indent=2))
if __name__ == "__main__":
main()
- 验证 `.tabs` 文本与 10 色清单、`tabs-icon-only`/`tabs-icon-top`/`tabs-icon-left`、`tabs-striped` + `tabs-top` + `tabs-background-*`/`tabs-color-*` 及单选激活切换。
所需环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。
当前源码 SHA256:cafc4ba16c56019771c3a866225e43fdadb0b5492192b3d22c338ba3728dcab7
本地运行
解压后按 README 说明在对应本地环境运行脚本。
所需环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。
ZIP SHA256:334b8a5c4c2c5d6f175c9477b0a36689df346bf9b7ae7d22d217f151e2b1bd43
常见错误与边界
- 实际端口、文档资源与应用配置都应和测试条件一致。
练习与解答
条纹样式选项卡如何分别控制背景色与前景色?当页面同时存在顶部标题栏与 `tabs-top` 时需要注意什么?
展开参考解答
通过 `tabs-background-{color}` 控制选项卡栏背景色,通过 `tabs-color-{color}` 控制文字、图标与激活条纹颜色;当顶部同时存在标题栏与 `tabs-top` 时,需为内容容器补充 `has-tabs-top`(及 `has-header`)以防止正文顶部被遮挡。