android - ViewPager 内的 TabLayout 不工作

标签 android android-viewpager android-tablayout

在此page Google 明确表示 TabLayout 可以在布局 xml 文件中集成到 ViewPager 中。

ViewPager integration

If you're using a ViewPager together with this layout, you can call setupWithViewPager(ViewPager) to link the two together. This layout will be automatically populated from the PagerAdapter's page titles.

This view also supports being used as part of a ViewPager's decor, and can be added directly to the ViewPager in a layout resource file like so:

<android.support.v4.view.ViewPager
 android:layout_width="match_parent"
 android:layout_height="match_parent">

 <android.support.design.widget.TabLayout
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:layout_gravity="top" />

 </android.support.v4.view.ViewPager>

但是,无论我怎样尝试,我都无法让 TableLayout 出现。两者分开就好了。我想知道这真的有效吗??

最佳答案

我不认为 TabLayout 像他们声称的那样作为 ViewPager 子项工作。

任何想要成为 ViewPager subview 的 View 都必须设置一个特定于 ViewPager 的 bool 布局参数属性,称为 isDecor。我刚刚查看了源代码,但没有看到 ViewPagerTabLayout 设置此布局参数属性的任何地方。

我认为这是他们打算做的事情,要么 a) 他们从未这样做过,要么 b) 出现问题所以他们将其删除。只需在 ViewPager 外部使用 TabLayout,将其作为装饰 View 并没有真正的好处。

关于android - ViewPager 内的 TabLayout 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38614184/

相关文章:

android - 如何避免向下滚动时隐藏选项卡?

java - If 语句在 Android OnClick 中不起作用

java - 无法对非静态方法进行静态引用 (Android getApplicationContext())

android - Realm 迁移时获取过去的数据库数据

android - 自定义支持对话框 fragment 文本颜色错误

android - 替换已弃用图库的选项

android - ViewPager 不会立即更新 textview 字体大小

android - 在viewpager android中滚动页面时更新可运行的后延迟秒数?

android - 如何在 TabLayout 中的选项卡之间添加边距?

android - 从待定意向打开特定选项卡