You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android TV应用:如何配置Leanback导航正确响应上下按键

解决方案

Leanback的焦点导航依赖视图的布局边界计算可跳转的焦点目标。你的问题核心是FeatureRow布局宽度未占满屏幕,导致焦点处于FeatureRow右侧元素时,向下方向无法识别到下方的ContentRow,必须先向左移动到布局覆盖区域内的位置才能跳转。通过XML设置FeatureRow为全屏宽度可解决该问题,具体步骤如下:

1. 修改FeatureRow Fragment的布局XML

确保FeatureRow的根布局宽度设为match_parent,让它占据整个屏幕横向空间,这样无论焦点在FeatureRow的哪个位置,向下方向都能检测到下方的ContentRow。

示例布局文件(fragment_feature_row.xml):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <!-- 横向展示3个推荐视频的HorizontalGridView -->
    <androidx.leanback.widget.HorizontalGridView
        android:id="@+id/feature_video_grid"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:clipToPadding="false"
        android:paddingStart="@dimen/leanback_horizontal_padding"
        android:paddingEnd="@dimen/leanback_horizontal_padding"/>
</LinearLayout>

关键注意点:

  • 根布局LinearLayout的android:layout_width必须设为match_parent,而非wrap_content
  • 内部的HorizontalGridView设置clipToPadding="false",保证视频内容不被屏幕边缘裁剪,同时不影响根布局的全屏宽度范围

2. 验证ContentRow的布局位置

确保ContentRow Fragment的布局在FeatureRow正下方,无横向偏移或margin阻断导航路径。建议ContentRow的根布局同样设为match_parent宽度,保持布局对齐。

3. 测试导航行为

修改后重新运行应用,在FeatureRow的任意视频元素上按下向下键,焦点应直接跳转到ContentRow的对应位置。

若问题仍存在,可尝试给FeatureRow的根布局添加android:focusable="true",确保布局本身参与焦点计算,但通常全屏宽度的设置已足够解决问题。

内容的提问来源于stack exchange,提问作者Joel Hager

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.11 04:53:18