首页 > 解决方案 > 如何在固定模式下将 tabLaout 的 tabitems 排列成多行?

问题描述

我想在 tabLayout 中有七个选项卡。它们将不可滚动。因此,我希望它们位于多行中。但是,在固定模式下,它们都排成一行。我找到了一些不是直接答案而是解决方法的答案。有没有办法做到这一点?

我的 XML 快照:

<com.google.android.material.tabs.TabLayout
        android:id="@+id/layout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabMode="fixed"
        app:tabSelectedTextColor="@color/colorGrayStandard"
        app:tabTextColor="@color/colorWhite">

        <com.google.android.material.tabs.TabItem
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/device" />

        <com.google.android.material.tabs.TabItem
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/wikipedia" />

        <com.google.android.material.tabs.TabItem
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/wikia" />

        <com.google.android.material.tabs.TabItem
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/youtube" />

        <com.google.android.material.tabs.TabItem
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/goodreads" />

        <com.google.android.material.tabs.TabItem
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/imdb" />

        <com.google.android.material.tabs.TabItem
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/yelp" />
    </com.google.android.material.tabs.TabLayout>

标签: androidandroid-tablayouttabitem

解决方案


这是 TabLayout 的文档:https ://developer.android.com/reference/com/google/android/material/tabs/TabLayout

TabLayout 提供了一个水平布局来显示选项卡。

这是文档中写的第一行,它是您对这个问题的回答。

TabLayout 无法做到这一点。您可以使它们可滚动,仅此而已。您可以创建和使用许多解决方法而不会出现问题。

在上面的文档中,您可以找到使用 TabLayout 实际可行的任何内容。如果您想创建解决方法,只需自己创建固定选项卡,创建侦听器,并在选项卡更改时使用片段来更改您想要的视图。


推荐阅读