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

ViewPager2迁移后页面标题无法显示的问题求助

ViewPager2迁移后页面标题无法显示的问题求助

我正在把我的App从ViewPager升级到ViewPager2,现在遇到了页面标题显示的问题,希望能得到帮助。

我按照Android官方的ViewPager2迁移指南(Java版)一步步操作,现在App能正常构建了,但之前的页面标题(显示当前页面是什么的头部)完全不显示了。原来用ViewPager的时候,我是搭配PagerTitleStrip来实现标题展示的,现在为了让App能编译通过,我已经把这部分XML代码注释掉了。

这是我当前展示不同Fragment页面的XML代码:

<androidx.viewpager2.widget.ViewPager2 xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <!--tools:context=".ETrainCommandMobile">-->
    <!-- This title strip will display the currently visible page title, as well as the page titles for adjacent pages. -->
    <!--<androidx.viewpager.widget.PagerTitleStrip
        android:id="@+id/pager_title_strip"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="top"
        android:background="#FF6633"
        android:clickable="false"
        android:paddingTop="4dp"
        android:paddingBottom="4dp"
        android:textColor="#FFEFD5"/>-->
</androidx.viewpager2.widget.ViewPager2>

你能看到整个PagerTitleStrip的XML代码都被我注释掉了。我了解到ViewPager2不允许包含任何子元素,也看到有人说需要把TabLayout和ViewPager2放在同一个父布局里来实现标题,但我不确定具体该怎么做,有点无从下手——毕竟我不是这方面的专家。

我已经完成了迁移指南里的所有步骤,除了添加TabLayout作为ViewPager2的父布局这部分,有点不敢随便改,怕出问题。现在真的不知道该怎么推进了,非常感谢能有人给我一些建议!

内容来源于stack exchange

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.07 10:59:50