如何在类似 Google Play 的标签页中启用水平滚动?

49

我正在参考一个关于Material Design的很棒的演示。它有选项卡,但当我添加太多的选项卡时,选项卡会被挤压(见截图)。我该怎么让它水平滚动?

enter image description here

我相信下面的布局是我应该进行更改的,但我查看了文档,似乎无法做到,请帮忙!

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            app:layout_scrollFlags="scroll|enterAlways" />

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

    </android.support.design.widget.AppBarLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="end|bottom"
        android:layout_margin="@dimen/fab_margin"
        android:src="@drawable/ic_done" />

</android.support.design.widget.CoordinatorLayout>

你尝试将TabLayout放入HorizontalScrollingView中了吗? - Damian Kozlak
嘿@N1to,那个有效!请将其留作答案,我会接受的。我只是用<HorizontalScrollView android:layout_width="match_parent" android:layout_height="match_parent">包装了TabLayout。唯一的问题是当我滑动到屏幕外的选项卡时,它不会聚焦在该选项卡上。 - TruMan1
3
不要这样做,TabLayout本身已经是一个水平滚动视图。你应该将TabLayout设置为MODE_SCROLLABLE(在文档中有说明)。 - BladeCoder
4个回答

139

TabLayout具有一个方法setTabMode(),可以是默认的MODE_FIXED,也可以是你需要的MODE_SCROLLABLE

你也可以在XML中使用app:tabMode="scrollable"来定义此选项。


请参考此博客以获取演示:http://www.ekiras.com/2015/12/how-to-implement-material-design-tab-layout-in-android.html - Ekansh Rastogi
在我的情况下,我在左侧使用TabLayout。是否可能在垂直方向滚动? - Md Imran Choudhury

5
您可以在您的android.support.design.widget.TabLayout中添加这个app:tabMode="scrollable"

9
你和被接受的答案有何不同? - ρяσѕρєя K

2
如果您添加了
app:tabMode="scrollable" 

如上所述,它展示了布局,但你无法滚动。

那么可能是因为你将它放在XML文件的顶部,并在它下面添加了ViewPager的match_parent(高度/宽度)。因为ViewPager位于选项卡布局之上,所以你将无法滚动。

将其添加到XML底部,这样另一个视图就不会重叠在选项卡布局上。


0

你需要的是SlidingTabLayout和SlidingTabStrip类。你可以从Google开发者网站复制这些类,将滑动选项卡布局添加到XML中,并在Java中为滑动选项卡布局设置ViewPager。希望能对你有所帮助。


我正在寻找如何从头开始制作这个控件的示例,这不是原生控件吗? - TruMan1
据我所知,这个控件不包含在库中,但是有其源代码。您需要将它们复制到您的项目中。https://developer.android.com/samples/SlidingTabsBasic/index.html - Sebastian Pakieła

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接