如何在Google Play等标签中启用水平滚动?

Tru*_*an1 35 android material-design android-design-library android-support-design

我在这里引用一个关于材料设计的精彩演示.它有标签,但是当我添加太多标签项时会被压扁(见截图).如何让它水平滚动?

在此输入图像描述

我相信下面是我应该做出改变的布局,但我梳理了文档并且似乎无法得到它,请帮忙!

<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>
Run Code Online (Sandbox Code Playgroud)

Lam*_*rak 110

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

您也可以使用XML定义它app:tabMode="scrollable".


Exe*_*lin 5

您可以将其添加app:tabMode="scrollable"到您的android.support.design.widget.TabLayout

  • 你的答案和接受的答案有什么区别? (9认同)