Skip to content

TV 首页:导航 + 多 Tab + 板块流 ​

复制下面的完整页面,替换文案与图片地址即可。顶部栏(品牌 / 频道 / 右上角动作组)、三个 Tab 的六列板块流、一行横向滚动的嵌套列表,以及加载 / 空 / 错误态、触底分页、焦点与三段式返回都已接好。所有尺寸来自包级主题 QTX_TV(从 @quicktvui/qtx 导入),页面不声明常量、模板不写魔法数字。组件参数以组件索引生成页为准,平台层尺寸与安全区见定量基线与 token JSON。

1. 完整页面(可复制) ​

vue
<template>
  <!-- TV 首页完整示例:顶部栏(品牌 / 频道 / 右上角动作组)+ 多 Tab 板块流。
       直接复制即可用:尺寸、间距、比例与颜色全部取自包级主题 QTX_TV,页面不声明常量。
       默认不裁切由 @quicktvui/qtx 构建插件在编译期补齐,业务不写裁切声明;见 /develop/focus-clipping。 -->
  <div class="tvh-page" :width="QTX_TV.screen.width" :height="QTX_TV.screen.height">

    <!-- ① 顶部栏:品牌靠左、频道栏居中、动作组收在右上角。
         动作组是 4 个 108×44 的小胶囊,不占独立工具条行,内容区从 QTX_TV.navHeight 直接开始。
         nav 与下面 qtx-tabs 写同名 linkId,焦点移动由 native 直接切 Tab。 -->
    <div class="tvh-top" :width="QTX_TV.screen.width" :height="QTX_TV.navHeight">
      <qt-text text="QTX TV" :width="QTX_TV.brand.width" :height="QTX_TV.brand.height" class="tvh-brand" />
      <div class="tvh-top-gap-left" :width="QTX_TV.topLeftGap" :height="QTX_TV.navBarHeight" />
      <qtx-channel-nav
        ref="navRef"
        :width="QTX_TV.channelNav.width" :height="QTX_TV.navBarHeight" :items="channels"
        :active="current" :focusedKey="focusedKey" linkId="tv-home"
        :focusWithin="navFocused"
        initialActiveKey="home" initialFocusKey="home"
        orientation="horizontal" :itemWidth="QTX_TV.channelNav.itemWidth" :itemHeight="QTX_TV.channelNav.itemHeight" :gap="QTX_TV.channelNav.itemGap"
        :fontSize="QTX_TV.channelNav.fontSize" :borderRadius="QTX_TV.channelNav.borderRadius"
        @change="onChannelChange" @focus="onNavFocus" @blur="onContentFocus"
      />
      <div class="tvh-top-gap-right" :width="QTX_TV.topRightGap" :height="QTX_TV.navBarHeight" />
      <div class="tvh-actions" :width="QTX_TV.action.groupWidth" :height="QTX_TV.navBarHeight">
        <qtx-button text="搜索" :width="QTX_TV.action.width" :height="QTX_TV.action.height" :fontSize="QTX_TV.action.fontSize" :cornerRadius="QTX_TV.action.cornerRadius"
          backgroundColor="#1E293B" textColor="#C7D2E0"
          focusBackgroundColor="#FFFFFF" focusTextColor="#0B1016"
          :focusable="true" @click="onSearch" />
        <div class="tvh-act-gap" :width="QTX_TV.action.gap" :height="QTX_TV.action.height" />
        <qtx-button :text="actLabels[0]" :width="QTX_TV.action.width" :height="QTX_TV.action.height" :fontSize="QTX_TV.action.fontSize" :cornerRadius="QTX_TV.action.cornerRadius"
          backgroundColor="#1E293B" textColor="#C7D2E0"
          focusBackgroundColor="#FFFFFF" focusTextColor="#0B1016"
          :focusable="true" @click="onAct1" />
        <div class="tvh-act-gap" :width="QTX_TV.action.gap" :height="QTX_TV.action.height" />
        <qtx-button :text="actLabels[1]" :width="QTX_TV.action.width" :height="QTX_TV.action.height" :fontSize="QTX_TV.action.fontSize" :cornerRadius="QTX_TV.action.cornerRadius"
          backgroundColor="#1E293B" textColor="#C7D2E0"
          focusBackgroundColor="#FFFFFF" focusTextColor="#0B1016"
          :focusable="true" @click="onAct2" />
        <div class="tvh-act-gap" :width="QTX_TV.action.gap" :height="QTX_TV.action.height" />
        <qtx-button :text="actLabels[2]" :width="QTX_TV.action.width" :height="QTX_TV.action.height" :fontSize="QTX_TV.action.fontSize" :cornerRadius="QTX_TV.action.cornerRadius"
          backgroundColor="#1E293B" textColor="#C7D2E0"
          focusBackgroundColor="#FFFFFF" focusTextColor="#0B1016"
          :focusable="true" @click="onAct3" />
      </div>
    </div>

    <!-- ② 内容区:每个频道一个页面实例,tab content 内只放一个 qtx-list。
         列表占满 QTX_TV.contentHeight,尺寸不因主题改变,导航隐藏后可直接全屏。 -->
    <qtx-tabs
      :width="QTX_TV.screen.width" :height="QTX_TV.contentHeight" :active="current"
      linkId="tv-home" :offscreen="2" :maxInstances="4"
      animationDirection="horizontal" @change="onTabChange"
    >
      <qtx-tab-content tabKey="home" :width="QTX_TV.screen.width" :height="QTX_TV.contentHeight"
        :focusable="true" @focus="onContentFocus">
        <qtx-list
          ref="homeListRef" class="tvh-feed" decoration="tv" poolKey="tv-home"
          :width="QTX_TV.screen.width" :height="QTX_TV.contentHeight"
          :items="homeRows" :columns="QTX_TV.grid.columns" key-field="id"
          :state="state" emptyText="这里还没有内容" loadingText="正在加载推荐…"
          errorText="加载失败,点击重试"
          :pageEnabled="true" :loadingTail="true" :initialFocusIndex="0"
          @item-click="onItemClick" @load-more="onLoadMore"
        >
          <!-- 内置样式行带 viewType,不走模板判别;但插槽必须保留,否则 ready 恒为 false。
               自定义行(k='rail')由插槽接管,内层再嵌一个横向 qtx-list 实现一行滚动。
               插槽是编译期原生模板,尺寸只能写字面量,数值与尺寸表 RAIL_* 一一对应。 -->
          <template #item="{ item }">
            <div v-if="item.k === 'rail'" :width="1688" :height="360" class="tvh-rail-row">
              <qt-text :text="item.label" :width="1688" :height="52" class="tvh-rail-label" />
              <qtx-list :columns="1" direction="horizontal" :items="item.channels"
                :width="1688" :height="288" class="tvh-rail-inner">
                <template #item="{ item: ch }">
                  <div :width="240" :height="272" class="tvh-rail-cell">
                    <qt-image :src="ch.image" :width="240" :height="180" class="tvh-rail-img" />
                    <qt-text :text="ch.title" :width="240" :height="48" class="tvh-rail-cell-t" />
                  </div>
                </template>
              </qtx-list>
            </div>
            <div v-else :width="260" :height="160">
              <qt-text text="内置样式行" :width="260" :height="60" />
            </div>
          </template>
        </qtx-list>
      </qtx-tab-content>

      <qtx-tab-content tabKey="series" :width="QTX_TV.screen.width" :height="QTX_TV.contentHeight"
        :focusable="true" @focus="onContentFocus">
        <qtx-list
          class="tvh-feed" decoration="tv" poolKey="tv-home"
          :width="QTX_TV.screen.width" :height="QTX_TV.contentHeight"
          :items="seriesRows" :columns="QTX_TV.grid.columns" key-field="id"
          :initialFocusIndex="0" @item-click="onItemClick"
        >
          <template #item>
            <div :width="260" :height="160">
              <qt-text text="内置样式行" :width="260" :height="60" />
            </div>
          </template>
        </qtx-list>
      </qtx-tab-content>

      <qtx-tab-content tabKey="apps" :width="QTX_TV.screen.width" :height="QTX_TV.contentHeight"
        :focusable="true" @focus="onContentFocus">
        <qtx-list
          class="tvh-feed" decoration="tv" poolKey="tv-home"
          :width="QTX_TV.screen.width" :height="QTX_TV.contentHeight"
          :items="appRows" :columns="QTX_TV.grid.columns" key-field="id"
          :state="appsState" emptyText="尚未安装任何应用" errorText="加载失败,点击重试"
          :initialFocusIndex="0" @item-click="onItemClick"
        >
          <template #item>
            <div :width="260" :height="160">
              <qt-text text="内置样式行" :width="260" :height="60" />
            </div>
          </template>
        </qtx-list>
      </qtx-tab-content>
    </qtx-tabs>
  </div>
</template>

<script setup lang="ts">
import { ref, computed } from 'vue';
import { useESRouter } from '@extscreen/es3-router';
import { QTX_TV, styledRows } from '@quicktvui/qtx';
import type { StyledRow } from '@quicktvui/qtx';

const router = useESRouter();

/* ── 全局主题:尺寸 / 间距 / 比例 / 颜色的唯一事实源在包里,不在页面里。
   页面写 import { QTX_TV } from '@quicktvui/qtx',模板直接取 QTX_TV.screen.width /
   QTX_TV.inset.h / QTX_TV.navHeight / QTX_TV.grid.columns …
   派生值(navHeight / contentHeight / topInnerWidth / topLeftGap / topRightGap /
   action.groupWidth)由主题内部算出,改一处自动重算,别在页面里手写算术。
   数值与官方依据见 /design/qtx-layout-tokens.json 的 appTheme。          */
/* 插槽内的编译期模板只认字面量,以下数值与 QTX_TV.rail 一一对应:
   width=1688,height=360,labelHeight=52,innerHeight=288,cellWidth=240,
   cellHeight=272,imageHeight=180,titleHeight=48,gap=8。
   这些字面量不能换成 QTX_TV.rail.* —— 插槽节点会被转成原生模板,
   成员表达式会触发 [QTX-T-EXPR] 回落。                                    */

/* ── 数据:行级 span 是「占几列」,取值在 [1, columns];aspectRatio 是宽 / 高。
   图片地址换成自己的 CDN;同一版块内比例保持一致。                        */
const IMG = 'https://picsum.photos/seed';

function poster(id: string, title: string, subtitle: string, seed: string,
  span: number, aspectRatio: number, posterType: string = 'text_bottom'): StyledRow {
  // text_float:常驻底部渐变遮罩 + 悬浮标题,适合主推位;
  // text_bottom:获焦放大时才在下方出现标题,适合密集海报墙。base 只显示图片。
  const row: StyledRow = {
    id, type: 'poster', image: `${IMG}/${seed}/640/360`,
    title, subtitle, posterType, span, aspectRatio,
  };
  if (posterType === 'text_float') {
    row.floatTitle = title;
    row.floatSubtitle = subtitle;
  }
  return row;
}

/** 一行横向滚动的嵌套列表行:不带 type,交给 #item 模板渲染内层 qtx-list。
 *  channels 是内层行数据;内层用 direction="horizontal" + columns=1 横向排列。 */
function railRow(prefix: string): StyledRow {
  return {
    id: `${prefix}-rail`,
    k: 'rail', // 自定义行判别键:内置样式行没有这个字段
    span: QTX_TV.grid.fullSpan,
    label: '继续观看 · 横向一行',
    channels: Array.from({ length: 10 }, (_, i) => ({
      id: `${prefix}-r${i}`,
      title: `影片 ${i + 1}`,
      image: `${IMG}/${prefix}-rail-${i}/320/180`,
    })),
  };
}

/**
 * 标准版块:两张主推 → 横向嵌套一行(仅首页)→ 版块标题 → 竖版海报 →
 * 留白 → 标题 → 应用图标行 → 状态行。
 * 横向 rail 紧跟主推位:Google TV 首页惯例,也保证下滑一屏就能看到这个能力。
 * 内置样式行用 styledRows() 生成;rail 行是自定义行,必须原样插入——
 * styledRows() 要求每行有已注册的 type,收不了它。
 */
function buildFeed(prefix: string, first: string, second: string,
  pack: 'glint' | 'compact' = 'glint', withRail = false): StyledRow[] {
  const head = styledRows([
    poster(`${prefix}-hero1`, '本周主推', '4K · 杜比视界', `${prefix}-hero1`, QTX_TV.grid.heroSpan, QTX_TV.grid.heroAspectRatio, 'text_float'),
    poster(`${prefix}-hero2`, '独家上线', '纪录片 · 2026', `${prefix}-hero2`, QTX_TV.grid.heroSpan, QTX_TV.grid.heroAspectRatio, 'text_float'),
  ], { pack });
  const tail = styledRows([
    { id: `${prefix}-h1`, type: 'header', title: first, span: QTX_TV.grid.fullSpan },
    poster(`${prefix}-p1`, '海岸线', '更新至 12 集', `${prefix}-p1`, QTX_TV.grid.posterSpan, QTX_TV.grid.posterAspectRatio),
    poster(`${prefix}-p2`, '深夜食堂', '全 24 集', `${prefix}-p2`, QTX_TV.grid.posterSpan, QTX_TV.grid.posterAspectRatio),
    poster(`${prefix}-p3`, '远山', '会员专享', `${prefix}-p3`, QTX_TV.grid.posterSpan, QTX_TV.grid.posterAspectRatio),
    { id: `${prefix}-s1`, type: 'spacer', span: QTX_TV.grid.fullSpan },
    { id: `${prefix}-h2`, type: 'header', title: second, span: QTX_TV.grid.fullSpan },
    { id: `${prefix}-i1`, type: 'app_icon', title: '视频', icon: `${IMG}/${prefix}-i1/200/200`, span: 1, aspectRatio: 1 },
    { id: `${prefix}-i2`, type: 'app_icon', title: '音乐', icon: `${IMG}/${prefix}-i2/200/200`, span: 1, aspectRatio: 1 },
    { id: `${prefix}-i3`, type: 'app_icon', title: '游戏', icon: `${IMG}/${prefix}-i3/200/200`, span: 1, aspectRatio: 1 },
    { id: `${prefix}-i4`, type: 'app_icon', title: '相册', icon: `${IMG}/${prefix}-i4/200/200`, span: 1, aspectRatio: 1 },
    { id: `${prefix}-i5`, type: 'app_icon', title: '设置', icon: `${IMG}/${prefix}-i5/200/200`, span: 1, aspectRatio: 1 },
    { id: `${prefix}-i6`, type: 'app_icon', title: '应用商店', icon: `${IMG}/${prefix}-i6/200/200`, span: 1, aspectRatio: 1 },
    { id: `${prefix}-s2`, type: 'spacer', span: QTX_TV.grid.fullSpan },
    { id: `${prefix}-end`, type: 'text', title: '已经到底了', span: QTX_TV.grid.fullSpan },
  ], { pack });
  return withRail ? [...head, railRow(prefix), ...tail] : [...head, ...tail];
}

const pack = ref<'glint' | 'compact'>('glint');
const homeRows = ref(buildFeed('home', '热播榜单', '常用应用', pack.value, true));
const seriesRows = buildFeed('series', '正在追', '猜你喜欢', 'compact');
const appRows = buildFeed('apps', '最近打开', '全部应用');

/* ── 右上角动作组:按钮数量固定 4 个,文案与行为按 current 切换。
   按钮宽 108px,文案统一两字,换自己的动作时不要写超过两字(会被截断)。
   产品页把这些换成搜索 / 筛选 / 排序入口即可,尺寸不用改。        */
type ListState = 'ready' | 'loading' | 'empty' | 'error';
const state = ref<ListState>('ready');
const appsState = ref<ListState>('ready');   // 默认有内容;点右上角「空态」才演示空态
const stateOrder: ListState[] = ['ready', 'loading', 'empty', 'error'];

const actLabels = computed(() => {
  if (current.value === 'home') return ['状态', '风格', '更多'];
  if (current.value === 'series') return ['未完', '排序', '更多'];
  return ['空态', '重载', '更多'];
});
function cycleState() {
  state.value = stateOrder[(stateOrder.indexOf(state.value) + 1) % stateOrder.length];
}
function setAppsState(next: ListState) { appsState.value = next; }
function onAct1() {
  if (current.value === 'home') cycleState();
  else if (current.value === 'series') onFilter();
  else setAppsState('empty');
}
function onAct2() {
  if (current.value === 'home') switchPack();
  else if (current.value === 'series') onFilter();
  else setAppsState('ready');
}
function onAct3() {
  if (current.value === 'home') appendSection();
  else onFilter();
}

/* ── 分页:触底追加行,不要重建整页。 ────────────────────────────────── */
const homeListRef = ref<any>(null);
let pageNo = 1;
let loadingMore = false;
function appendSection() {
  if (loadingMore) return;
  loadingMore = true;
  const extra = buildFeed(`page-${pageNo}`, `第 ${pageNo + 1} 页推荐`, '更多应用', pack.value);
  homeRows.value = homeRows.value.concat(extra);
  pageNo += 1;
  loadingMore = false;
}
function onLoadMore() {
  if (state.value !== 'ready' || pageNo >= 4) {
    void homeListRef.value?.setLoading?.(false);
    return;
  }
  appendSection();
  void homeListRef.value?.setLoading?.(false);
}

/* ── 导航与焦点:linkId 负责切 Tab,方向键只移动焦点。
   change 回调同值回写是幂等的,用它做数据加载、返回栈、按 tabId 状态分桶与埋点。 */
const navRef = ref<any>(null);
const channels = [
  { key: 'home', label: '首页' },
  { key: 'series', label: '电视剧' },
  { key: 'apps', label: '应用' },
];
const current = ref('home');
const focusedKey = ref('home');
const navFocused = ref(true);
let focusIndex = 0;

function onNavFocus() { navFocused.value = true; }
function onContentFocus() { navFocused.value = false; }

function onChannelChange(key: string, payload: any) {
  current.value = key;
  loadTabData(key, payload && payload.source);
}
function onTabChange(key: string) { current.value = key; }
function loadTabData(key: string, source: string) {
  // payload.source: focus(焦点移动)/ click(确认或点击)/ api / back
  void key; void source;
}

function onKeyDown(e: any): void {
  if (!e || Number(e.action) !== 0) return;
  if (!navFocused.value) return; // 焦点在内容区时左右键属于内容区
  const code = Number(e.keyCode);
  const step = code === 22 ? 1 : code === 21 ? -1 : 0; // DPAD_RIGHT / LEFT
  if (!step) return;
  const next = Math.max(0, Math.min(channels.length - 1, focusIndex + step));
  focusIndex = next;
  focusedKey.value = channels[next].key;
}

/* ── 返回链:三段式 —— 焦点先回频道栏 → 列表回顶 → 频道归位 → 页面退出。
   这一行必须保留;写成空实现时焦点落在内容区就收不到 BACK。             */
function onBackPressed() {
  if (navRef.value && navRef.value.handleBack && navRef.value.handleBack()) return;
  router.back();
}
// 宿主按精确名字取页面级回调;<script setup> 不暴露时焦点落在内容区就收不到 BACK。
defineExpose({ onBackPressed });

/* ── 交互:确认键进入详情;错误态点击重试。 ─────────────────────────── */
function onItemClick(e: any) {
  if (state.value === 'error') { state.value = 'ready'; return; }
  const id = e && e.item ? e.item.id : '';
  if (id) router.push({ name: 'detail', params: { id: String(id) } });
}
function onSearch() {
  router.push({ name: 'search', params: { from: current.value } });
}
function onFilter() {}
function switchPack() {
  pack.value = pack.value === 'glint' ? 'compact' : 'glint';
  homeRows.value = buildFeed('home', '热播榜单', '常用应用', pack.value, true);
}
</script>

<style>
/* 容器尺寸由模板的 :width / :height 给出,这里只写颜色、排版与留白。
   .tvh-top 的左右 padding = QTX_TV.inset.h、上 padding = QTX_TV.topSafeV;改尺寸表时同步这一行。 */
.tvh-page { width: 1920px; height: 1080px; background-color: #0B1016; flex-direction: column; padding-left: 0px; padding-right: 0px; padding-top: 0px; padding-bottom: 0px; }
/* padding-top = QTX_TV.topSafeV = 56:官方 28dp×2 推荐内容边距,同时高于 27dp×2 过扫描安全线。
   内容区高度 = QTX_TV.navHeight - QTX_TV.topSafeV = 72,正好等于 QTX_TV.navBarHeight,导航行不会挤到屏幕边缘。 */
.tvh-top { width: 1920px; height: 128px; flex-direction: row; align-items: center; padding-left: 116px; padding-right: 116px; padding-top: 56px; }
.tvh-brand { width: 240px; height: 64px; font-size: 40px; color: #FFFFFF; }
.tvh-top-gap-left { width: 244px; height: 72px; }
.tvh-top-gap-right { width: 22px; height: 72px; }
.tvh-actions { width: 462px; height: 72px; flex-direction: row; align-items: center; }
.tvh-act-gap { width: 10px; height: 44px; }
</style>

<!-- 两个 <style> 都不写 scoped:原生节点(qtx-list)与 #item 插槽转出的
     编译期模板节点都不带 data-v,scoped 选择器匹配不到 —— 前者会露出异色底色,
     后者内层列表高度会变成 0。所有类名带 tvh- 前缀:demo / 业务工程里常有
     非 scoped 的 .feed { margin-top: 72px } 一类全局样式,用通用类名会被污染,
     在导航和内容之间凭空多出一段空白。 -->
<style>
/* 列表底色与页面同色;非 active Tab 由 qtx-tabs 收敛为 INVISIBLE,底色不承担遮挡职责 */
.tvh-feed { background-color: #0B1016; }
.tvh-rail-row { width: 1688px; height: 360px; background-color: #16202C; flex-direction: column; border-radius: 12px; }
.tvh-rail-label { width: 1688px; height: 52px; font-size: 30px; color: #FFFFFF; margin-left: 24px; margin-top: 12px; }
.tvh-rail-inner { width: 1688px; height: 288px; margin-top: 8px; }
.tvh-rail-cell { width: 240px; height: 272px; background-color: #1E293B; border-radius: 8px; margin-left: 8px; flex-direction: column; }
.tvh-rail-img { width: 240px; height: 180px; border-radius: 8px; }
.tvh-rail-cell-t { width: 240px; height: 48px; font-size: 24px; color: #FFFFFF; text-align: center; margin-top: 12px; }
</style>

这一个文件里已经接好:

位置写法作用
顶部qtx-channel-nav 与 qtx-tabs 写同名 linkId焦点移动由 native 直接切 Tab,页面只维护 focusedKey
顶部initialActiveKey / initialFocusKey / focusWithin进页默认频道、默认焦点、焦点离开 nav 时自动转为选中态
右上角4 个 108 × 44 胶囊按钮,actLabels 按 current 换文案动作组不占独立工具条行,内容区从 QTX_TV.navHeight 直接开始;换自己的动作不用改尺寸
列表decoration="tv" + :columns="6"116 / 56px 内容边界与 16 / 32px 卡间距 / 行间距由列表内部承担,list 尺寸不变、可全屏
数据buildFeed() + styledRows() + 行级 span通栏主推、版块标题、竖版海报、应用图标、留白与状态行组成流式板块
海报posterType: 'text_float' / 'text_bottom'前者常驻渐变遮罩与标题,后者获焦放大时才在下方显示标题
嵌套一行#item 插槽里放 direction="horizontal" 的内层 qtx-list首页「继续观看」横向滚动 rail,一行 10 张卡片,紧跟两张主推图之后
状态:state + emptyText / loadingText / errorText加载 / 空 / 错误 / 正常四态,不切换 qtx-tab-content 的子节点类型
分页:pageEnabled + :loadingTail + @load-more触底追加一个板块,不重建整页
切换动画qtx-tabs 默认 animationDuration: 300横向平移切页;写 :animationDuration="0" 可硬切
交互@item-click、onKeyDown、onBackPressed确认进详情、左右键移焦点、三段式返回

全局主题 QTX_TV(从 @quicktvui/qtx 导入,页面只引用、不声明常量;下表是逐项取值与依据):

主题字段值作用
QTX_TV.screen.width × height1920 × 1080页面根容器,所有标签必须显式给宽高
QTX_TV.inset.h / v116 / 56与 decoration="tv" 同源的内容边界;来自官方推荐布局边距 58dp / 28dp(1920 × 1080 下 ×2),不要另给 qtx-list 写 margin
QTX_TV.topSafeV56顶部安全区:官方推荐布局边距 verticalDp=28 × 2;也高于过扫描安全线 verticalDp=27 × 2 = 54。交互元素不得越过这条线
QTX_TV.navBarHeight72导航行高度(频道胶囊高)
QTX_TV.navHeight128topSafeV + navBarHeight;顶部栏上留白 56,不再垂直居中把元素推到距顶 30px
QTX_TV.contentHeight952screen.height - navHeight,列表占满内容区;导航隐藏后可直接全屏
QTX_TV.brand.width × height240 × 64左上角品牌文字
QTX_TV.channelNav.*720 / 160 / 72 / 12频道栏容器与单项尺寸(width / itemWidth / itemHeight / itemGap);另有 itemPadding: 16、iconMargin: 16。这是紧凑顶栏预设,显式传给组件;组件不写样式 props 时默认更宽松,见组件页
QTX_TV.action.width × height / gap / count108 × 44 / 10 / 4右上角胶囊按钮;文案统一两字,超两字会被截断
QTX_TV.action.groupWidth462由 count × width + (count-1) × gap 算出,搜索 + 3 个频道动作
QTX_TV.topLeftGap / topRightGap244 / 22由「频道栏居中」在主题内部反推,页面不用手调
QTX_TV.grid.columns6span 的分母
QTX_TV.grid.heroSpan / heroAspectRatio3 / 1.78一行两张 16:9 主推
QTX_TV.grid.posterSpan / posterAspectRatio2 / 0.667一行三张 2:3 竖版海报
QTX_TV.grid.fullSpan6通栏:版块标题、留白、状态行
QTX_TV.rail.* / color.* / font.*见主题字段表横向一行的卡片尺寸、色板与字号

派生值(navHeight / contentHeight / topInnerWidth / topLeftGap / topRightGap / action.groupWidth / action.cornerRadius)由基础值在主题构造时算出,改基础值自动重算。按钮数不是 4 时调 qtxTvTopGaps(2) 拿 { groupWidth, left, right },频道栏仍居中。页面允许保留的「本页选择」只有按钮数量与栅格列数。

QTX_TV.topInnerWidth = QTX_TV.screen.width - 2 × QTX_TV.inset.h = 1688;左上角品牌、右上角动作组都落在 116px 内容边界上。垂直节奏按 Google TV 对齐:QTX_TV.topSafeV = 56(官方 recommendedLayoutMargin.verticalDp = 28 × 2,同时满足 overscanMargin.verticalDp = 27 × 2 = 54 的最小安全线)→ 导航行 72 → 列表内部 insetV = 56 → 第一行内容。于是「屏幕顶 → 导航」和「导航 → 第一行内容」都是 56px,不再出现上紧下松或中间夹一条异色横带;不要在 QTX_TV.navHeight 之外再写 margin-top 补间距,那会让两段留白叠加。

QTX_TV.topLeftGap 让频道栏在整行里居中,改 QTX_TV.channelNav.width / QTX_TV.brand.width / QTX_TV.action.width 时它和 QTX_TV.topRightGap 会自动重算,不要写死。qtx-tab-content 内只放一个 qtx-list。根容器显式写 padding: 0(理由见第 4 节)。列表底色必须写在非 scoped 的 <style> 块里:qtx-list 是原生节点、scoped 选择器匹配不到,写在 scoped 块里会露出与页面不同的底色,在导航与内容之间形成一条横带。所有类名带 tvh- 前缀:宿主工程常有非 scoped 的 .feed { margin-top: 72px } 一类全局样式,用通用类名会被命中,凭空多出一段空白。数值口径见TV 装饰预设。

换屏高 / 色板时在业务工程里定义同结构的主题对象替换 QTX_TV 引用,并同步 .page / .top 的 CSS 宽高、左右 padding 与颜色字面量。#item 插槽是编译期原生模板,尺寸只能写字面量,改 RAIL_* 时同步改插槽内的 :width / :height 与非 scoped 样式。

增加频道:channels 加一项 → qtx-tabs 里复制一个 qtx-tab-content 块并改 tabKey → 用 buildFeed() 再生成一份 rows。频道 key 必须与 tabKey 一致。

2. 常见改法 ​

要改成改哪里
左侧竖排导航qtx-channel-nav 设 orientation="vertical"、:width="260" :height="1080",qtx-tabs 宽度改为 1600;左右键(21 / 22)换成上下键(19 / 20)。完整竖排写法见频道导航示例
换右上角动作改 actLabels 与 onAct1..3 的分发;按钮数与 QTX_TV.action.width 不变则所有尺寸常量不用动
换风格styledRows(buildFeed(...), { pack: 'compact' }),两套风格见内置 item 样式
让海报常驻标题poster() 传 'text_float',并给 floatTitle / floatSubtitle;'text_bottom' 只在获焦时显示标题
换卡型改行的 type,可用 6 种见内置 item 参考
改横向一行的卡片改 railRow() 的 channels 与 #item 里的内层模板;channels 数量决定可横向滚动的宽度
去掉横向一行buildFeed(..., withRail = false);自定义行不经过 styledRows()
显示加载 / 空 / 错误态改 :state 为 loading / empty / error,文案由 loadingText / emptyText / errorText 给出;错误态点击可在 onItemClick 里还原
触底加载更多保留 :pageEnabled 与 @load-more,把 appendSection() 换成自己的接口调用;数据只追加、不重建整页
加分页往 rows 数组追加行,不要整页重建;首次滚动回调即满足阈值,触底加载要加守卫,见 QtxList 指南
接自己的路由onItemClick 中的 name / params、onSearch 的搜索页,以及 router.back()
焦点移动即切 Tab给 qtx-channel-nav 与 qtx-tabs 写同名 linkId,页面 onKeyDown 只维护 focusedKey、不用回写 active 驱动切换;接线见频道导航示例
返回先回默认频道给 qtx-channel-nav 写 initialActiveKey / initialFocusKey,页面 onBackPressed 里先调 navRef.handleBack(),返回 true 就消费本次返回;要关闭写 disableBackToInitial,详见初始位置与返回归位

动作组与工具条的两种放法 ​

主示例把动作组收在顶部栏右上角:4 个 108 × 44 的胶囊按钮,与品牌、频道栏同一行,不占独立工具条行,内容区从 QTX_TV.navHeight 直接开始、占满 QTX_TV.contentHeight。按钮数量固定,文案与行为用 actLabels / onAct1..3 按 current 分发,换自己的动作时尺寸不用改;文案统一两字,超过两字会被截断。真要放长文案,把 QTX_TV.action.width 调大并同步 QTX_TV.action.groupWidth、QTX_TV.topRightGap 与 .tvh-actions / .tvh-act-gap 的 CSS 宽度。

如果工具条需要随 Tab 实例一起缓存(切回来时排序、布局、焦点与滚动位都保留),就把工具条放进 qtx-tab-content:<qtx-tab-content> 接受多个同级子 view,工具条用 position-type: absolute 固定在顶部,列表用 margin-top 让出工具条高度、以 QTX_TV.contentHeight - TOOLBAR_H - NAV_TO_CONTENT 占满剩余高度;顶部安全区仍用 QTX_TV.topSafeV=56,QTX_TV.navHeight 与 QTX_TV.contentHeight 由同一套常量推导。完整契约见 QtxTabs 指南 的「Tab 内容子节点的布局契约」。

vue
<template>
  <!-- 每个 Tab 自带工具条:工具条属于 tab content,不属于全局壳。
       屏高与工具条高度只在进页时确定;列表高度由表达式推导,换屏高只改 QTX_TV.contentHeight。 -->
  <div class="page" :width="1920" :height="1080">
    <div class="top" :width="1920" :height="QTX_TV.navHeight">
      <qtx-button text="返回" :width="120" :height="64" :autofocus="true" @click="onBackPressed" />
      <qtx-channel-nav
        :width="1800" :height="QTX_TV.navBarHeight" :items="channels" :active="current" :focusedKey="focusedKey"
        orientation="horizontal" :itemWidth="160" :itemHeight="72" :gap="12"
        @change="onChannelChange"
      />
    </div>

    <qtx-tabs :width="1920" :height="QTX_TV.contentHeight" :active="current"
      :offscreen="2" :maxInstances="4" @change="onTabChange">

      <!-- ① 历史:固定流式工具条,动作集只有清空 / 时间范围 -->
      <qtx-tab-content tabKey="history" :width="1920" :height="QTX_TV.contentHeight">
        <div class="toolbar" :width="1688" :height="TOOLBAR_H">
          <qt-text text="历史工具条:清空 / 时间范围" :width="1000" :height="56" class="tb-t" />
          <qtx-button text="清空历史" :width="220" :height="56" :focusable="true"
            @click="onAction('history')" />
        </div>
        <qtx-list class="feed" :width="1920" :height="QTX_TV.contentHeight - TOOLBAR_H - NAV_TO_CONTENT"
          :items="historyRows" :columns="3" key-field="id" @item-click="onItemClick">
          <template #item="{ item }">
            <div class="cell" :width="600" :height="200">
              <qt-text :text="item.title" :width="600" :height="200" class="cell-t" />
            </div>
          </template>
        </qtx-list>
      </qtx-tab-content>

      <!-- ② 本地目录:动作集完全不同,壳不认识任何业务词 -->
      <qtx-tab-content tabKey="local" :width="1920" :height="QTX_TV.contentHeight">
        <div class="toolbar" :width="1688" :height="TOOLBAR_H">
          <qt-text text="本地工具条:面包屑 / 排序 / 视图" :width="1000" :height="56" class="tb-t" />
          <qtx-button text="排序" :width="180" :height="56" :focusable="true" @click="onSort" />
          <qtx-button text="网格⇄列表" :width="220" :height="56" :focusable="true" @click="onLayout" />
        </div>
        <qtx-list class="feed" :width="1920" :height="QTX_TV.contentHeight - TOOLBAR_H - NAV_TO_CONTENT"
          :items="localRows" :columns="3" key-field="id" @item-click="onItemClick">
          <template #item="{ item }">
            <div class="cell" :width="600" :height="200">
              <qt-text :text="item.title" :width="600" :height="200" class="cell-t" />
            </div>
          </template>
        </qtx-list>
      </qtx-tab-content>
    </qtx-tabs>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue';
import { useESRouter } from '@extscreen/es3-router';
import { QTX_TV, QTX_TV_PAGE_RHYTHM } from '@quicktvui/qtx';

const router = useESRouter();

// 进页时确定,运行时不要改:换屏高只改 QTX_TV.contentHeight,列表高度自动推导
// 顶部安全区 / 导航行 / 内容区高度全部取自包级主题 QTX_TV。
// 这一页是「工具条放进 tab-content 随实例缓存」的另一种放法,才额外用到
// QTX_TV_PAGE_RHYTHM 的工具条高度与导航↔内容间距。
const TOOLBAR_H = QTX_TV_PAGE_RHYTHM.toolbarHeightPx;
const NAV_TO_CONTENT = QTX_TV_PAGE_RHYTHM.navToContentGapPx;

const channels = [
  { key: 'history', label: '历史' },
  { key: 'local', label: '本地目录' },
];
const current = ref('history');
const focusedKey = ref('history');

// 每个 Tab 的状态按 tabId 分桶;内容壳常驻,切回来不丢
const tabState = reactive<Record<string, { sort: string; layout: string }>>({
  history: { sort: '时间↓', layout: '流式' },
  local: { sort: '名称↑', layout: '网格' },
});

function makeRows(prefix: string): Array<{ id: string; title: string }> {
  const rows: Array<{ id: string; title: string }> = [];
  for (let i = 0; i < 12; i++) {
    rows.push({ id: `${prefix}-${i}`, title: `${prefix} 卡片 ${i + 1}` });
  }
  return rows;
}
const historyRows = makeRows('历史');
const localRows = makeRows('本地');

function onChannelChange(key: string) {
  current.value = key;
  focusedKey.value = key;
}
function onTabChange(key: string) {
  current.value = key;
}
function onBackPressed() {
  router.back();
}
// 宿主按精确名字取页面级回调;<script setup> 必须显式暴露。
defineExpose({ onBackPressed });
function onItemClick(e: any) {
  const id = e && e.item ? e.item.id : '';
  if (id) router.push({ name: 'detail', params: { id: String(id) } });
}
function onSort() {
  const s = tabState[current.value];
  s.sort = s.sort === '名称↑' ? '名称↓' : '名称↑';
}
function onLayout() {
  const s = tabState[current.value];
  s.layout = s.layout === '网格' ? '列表' : '网格';
}
function onAction(key: string) {
  tabState[key].layout = '流式';
}
</script>

<style scoped>
.page { width: 1920px; height: 1080px; background-color: #0B1016; flex-direction: column; padding-left: 0px; padding-right: 0px; padding-top: 0px; padding-bottom: 0px; }
.top { width: 1920px; height: 128px; flex-direction: row; align-items: center; padding-top: 56px; }
/* left 116 对齐 decoration 内容边界,top 32 对应 NAV_TO_CONTENT;标题↔动作 24 */
.toolbar { width: 1688px; height: 72px; flex-direction: row; align-items: center; background-color: #334155; position-type: absolute; left: 116px; top: 32px; }
.tb-t { width: 1000px; height: 56px; font-size: 26px; color: #FFFFFF; margin-right: 24px; }
/* margin-top 让出工具条与导航间距:qtx-list 有内容时会覆盖前一位同级子 view */
.feed { width: 1920px; margin-top: 104px; background-color: #1E293B; }
.cell { width: 600px; height: 200px; background-color: #2563EB; }
.cell-t { width: 600px; height: 200px; font-size: 30px; color: #FFFFFF; text-align: center; }
</style>

3. 行数据怎么填 ​

列表为 6 列。行级 span 是「占几列」,取值被约束在 [1, columns]:

span占位用途
6整行版块标题、留白行、通栏大图、状态行
3半行主推宽幅卡片(一行两张)
2三分之一行竖版海报(一行三张)
1六分之一行小图标、密集网格

aspectRatio 是「宽 / 高」,单元格高度按 宽度 / aspectRatio 计算。内置样式行必须显式给出,否则卡片高度随内容塌陷。取值:1.78(16:9)、1(1:1)、0.667(2:3),同一版块内保持一致。平台 12 栏栅格用于页面级内容边界与对齐,与列表列数不是同一单位。

类型用途建议 span建议 aspectRatio
header版块标题,整行且不可获焦6—
poster海报卡片,主要展示单元2 / 3 / 60.667 / 1.78
image纯图片素材,字段与 poster 相同3 / 6按素材比例
text状态化文本、空态提示6—
spacer版块之间的物理留白6—
app_icon应用入口图标1 / 2按风格包

标准版块顺序:两张主推 poster(span=3, aspectRatio=1.78) → 首页追加一个横向嵌套 rail → header(span=6) → 若干 poster(span=2, aspectRatio=0.667) → spacer(span=6) → 第二个 header → 六个 app_icon(span=1) → spacer → text 状态行。横向 rail 紧跟主推位,是为了让下滑一屏就能看到这行能力,不要把它埋到竖版海报之后;改顺序只动 buildFeed() 的拼接,不动行的 span。字段只写原生实际读取的键,完整清单与「协议字段」见内置 item 参考;例如 text 类型读取 title,header 只有 title 生效,score / progress / fontSize 一类写入不产生效果。

4. 必须写对的几件事 ​

  • 所有标签显式声明 width / height;根容器 1920 × 1080;顶部交互元素距屏幕上边缘不小于 56px(官方推荐布局边距 28dp × 2,且高于过扫描线 27dp × 2 = 54px),数值以 android-tv-tokens.json 的 safeArea 为准。不要靠「在 132px 栏内垂直居中」把元素推到距顶 30px。
  • 不给 qtx-list 写左右 margin,也不给 qtx-tab-content 写 padding / margin —— 内容边界由 decoration="tv" 在列表内部承担;顶部栏左右 padding 取 QTX_TV.inset.h,品牌与右上角动作组因此与内容边界对齐。
  • 垂直节奏只有两段:顶部安全区 QTX_TV.topSafeV=56、导航行 QTX_TV.navBarHeight=72,内容区从 QTX_TV.navHeight=128 开始,列表自身 insetV=56 承担导航↔内容间距。不要另加 margin-top,两段留白叠加会把导航和内容拉得过远。
  • 根容器显式写 padding-left/right/top/bottom: 0px。宿主可能给页面容器带默认内边距,1920 宽的内容区会被整体推偏并溢出右侧,116px 的左右边界看起来就不对称了。
  • 作用在 qtx-list 与 #item 插槽上的样式写在非 scoped 的 <style> 块里;原生节点与编译期模板节点都不带 data-v,scoped 匹配不到会露出默认底色、让内层列表高度为 0。类名必须带页面专属前缀(tvh-),否则会被宿主工程非 scoped 的 .feed { margin-top: 72px } 一类全局样式污染。
  • qtx-tabs 默认走 300ms 横向平移动画(显式写 :animationDuration="0" 才是硬切);非 active Tab 由组件收敛为 INVISIBLE,稳态只有 active 页绘制,业务不用自己处理显隐。qtx-list 可写与页面同色的底色保持深色统一,但它不承担遮挡职责。
  • 页面默认不裁由构建插件在编译期补齐,示例不写任何裁切属性;接入与自检见焦点放大与容器裁切。
  • current 同源驱动频道栏与 qtx-tabs,频道栏的 change 负责回写;qtx-tabs 只做页面实例缓存与切换,不渲染频道项。
  • #item 占位模板必须保留:内置样式行带 viewType、不走模板判别,但没有这个插槽时列表 ready 恒为 false、内容不下发。
  • 每页至少一个 :autofocus="true" 节点;横排频道栏用左右键维护 focusedKey,确认键才切频道;返回键按「内容区 → 导航栏 → 主频道 → 交还系统」处理。
  • 状态切换用 qtx-list 的 state 属性,不要在 qtx-tab-content 里用 v-if 切换子节点类型。
  • 横向嵌套一行用「外层自定义行 + #item 插槽里的内层 qtx-list」:自定义行不带 type,不能过 styledRows();内层只写 columns / direction / items / 尺寸 / class 这些编译期白名单属性,key-field 等外层属性会触发 [QTX-T-EXPR] 回落。#item 插槽内的节点由编译期转成原生模板,不带 scoped 的 data-v 属性,所以嵌套行的样式必须写在非 scoped 的 <style> 块里;写成 scoped 时样式全部不匹配,内层列表高度为 0、卡片不显示。完整契约见 QtxList 指南 的嵌套子列表章节。
  • 追加数据不重建整页;空态 / 错误态 / 加载态由 state 表达,并保留一个可获焦的下一步动作。
  • 模板注释里不要写字面量 <script>:编译期会给每个 qtx-list 注入 tpl-key 与模板注册表,注入点按首个 script 块定位;注释里出现这个字样会让注册表被写进注释文本,三个列表全部查不到模板数据并静默回落到运行时捕获,横向嵌套一行会整段失效。编译器已跳过注释做定位,文案里也别再引入这个歧义。
  • 本库没有 carousel 组件;需要轮播主推位时用满列 poster / image 行承担,或按平台能力自行扩展。
  • 焦点三态(普通 / 焦点 / 选中)正交叠加;当前频道重新获焦时底色与描边应同时可见。

5. 生成后自检 ​

  • [ ] 顶部交互元素距屏幕上边缘 ≥56px(官方 28dp×2,过扫描线 27dp×2=54);导航↔第一行内容约 56px,两段留白对称、中间没有异色横带。
  • [ ] 间距不靠魔法数字:尺寸全部取自 QTX_TV,页面没有声明 SCREEN_W / INSET_H / BTN_W 一类常量;qtx-list 写 decoration="tv" 取内容边界与卡片间距;数值与官方 / qtx 口径见TV 装饰预设。
  • [ ] 根容器 padding 四项显式清零,内容左右边界对称(1920 宽下约 116 / 116)。
  • [ ] 切 Tab 有平移动画;连续切换所有频道再切回,画面与首次进页一致、无其它 Tab 的文字或卡片残影。
  • [ ] 频道 key 与 tabKey 一一对应,current 同时驱动两处;用 linkId 时两处写同名,且 @change 仍负责数据加载与状态保存。
  • [ ] 用 linkId 时先向下滚动一段再连续按上键:焦点应先在列表内逐行上移、列表同步滚回,滚到最顶部 item 后才回到频道栏当前选中项;中途不跳导航、不落到右上角动作组,current 不变化。改成左侧竖排频道栏时,出界方向自动变为左键、进内容方向自动变为右键,其余方向(含横向 rail、分页)仍按 qtx-list 原逻辑。焦点在频道栏时朝内容方向按键应落到内容区、落点由几何决定。若按 工具条放进 tab content 的写法在导航与列表之间加了工具条,焦点应逐层经过工具条而不是跳过它。若要恢复平台几何寻焦,给 qtx-tabs 写 :focusBoundary="false"。
  • [ ] 在列表里向下滚一段后切换频道、再切回:内容从顶部重新开始,不保留上次滚动位置;不写 linkId 的页面滚动位置保持原样。
  • [ ] 右上角动作组与品牌、频道栏同在 QTX_TV.navHeight 一行,按钮文案不超两字;qtx-tabs 与列表用 QTX_TV.contentHeight 占满内容区,屏高未在运行时改变;若把工具条放进 tab content,按 tab-toolbar 示例用绝对定位 + 列表 margin-top。
  • [ ] 横向嵌套一行显示出一排卡片,日志有 nested factory registered、setNestedItems;模板注释里没有字面量 <script>。
  • [ ] 所有标签有显式宽高,每页有自动获焦节点,返回键与方向键已接线,onBackPressed 里保留了 navRef.handleBack();用 <script setup> 时还要 defineExpose({ onBackPressed }),否则宿主取不到回调、按返回会直接退出应用。
  • [ ] 每行 span 在 [1, columns] 内;header / spacer / 通栏行用满列值。
  • [ ] 所有内置样式行显式给出 aspectRatio,同一版块比例一致;#item 占位模板保留。
  • [ ] 海报按需要选 posterType:text_float 常驻标题需给 floatTitle / floatSubtitle,text_bottom 只在获焦时显示标题。
  • [ ] 字段来自内置 item 参考的有效键;加载 / 空 / 错误态用 state,没有在 qtx-tab-content 里用 v-if 切换子节点类型。
  • [ ] 横向嵌套行是自定义行、没过 styledRows();内层 qtx-list 没写 key-field 等外层属性;嵌套行样式在非 scoped 的 <style> 块里,内层卡片可见且左右键能横向滚动。
  • [ ] 分页走追加;构建输出无 [QTX-T-EXPR] 回落。

API 事实由源码自动生成 · 请勿手工编辑