使用设计支持库,选项卡不占用全宽

Tabs don't take full width using Design Support Library

我正在关注 this tutorial,它解释了如何实现 Google 设计支持库中包含的新 TabLayout。这是我的应用程序:

如上面的屏幕截图所示,我的选项卡没有占据整个屏幕宽度。

我该如何解决?

这是我的 activity xml: (我的 Java 代码与 the tutorial 中的代码相同。在下面的代码中,我的 tablayout 和 viewpager 在 FrameLayout 中)

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">
<!-- your content layout -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    <!-- Toolbar instead of ActionBar so the drawer can slide on top -->
    <!-- If you want to have dark background for options buttons remove the popup theme -->
        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            android:layout_width="match_parent"
            android:layout_height="@dimen/abc_action_bar_default_height_material"
            android:background="?attr/colorPrimary"
            android:minHeight="?attr/actionBarSize" />
    <!-- Real content goes here -->
        <FrameLayout
            android:id="@+id/content_frame"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1" >
            <android.support.design.widget.TabLayout
                android:id="@+id/sliding_tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:tabMode="scrollable" />
            <android.support.v4.view.ViewPager
                android:id="@+id/viewpager"
                android:layout_width="match_parent"
                android:layout_height="0px"
                android:layout_weight="1"
                android:background="@android:color/white" />
        </FrameLayout>
    </LinearLayout>
    <android.support.design.widget.NavigationView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:id="@+id/nav_view"
        app:headerLayout="@layout/nav_view_header"
        app:menu="@layout/nav_view_menu"
        app:theme="@style/MyTheme.NavMenu" />
</android.support.v4.widget.DrawerLayout>

编辑:

添加 app:tabGravity="center" 并将 app:tabMode 更改为 fixed 后,应用程序的外观如下:

我需要它们来填充宽度。

这个问题不是重复的。另一个答案没有解决我的问题。

http://developer.android.com/reference/android/support/design/widget/TabLayout.html

GRAVITY_CENTER Gravity used to lay out the tabs in the center of the TabLayout.

GRAVITY_FILL Gravity used to fill the TabLayout as much as possible.

MODE_FIXED Fixed tabs display all tabs concurrently and are best used with content that benefits from quick pivots between tabs.

MODE_SCROLLABLE Scrollable tabs display a subset of tabs at any given moment, and can contain longer tab labels and a larger number of tabs.

只需在您的 TabLayout 中设置:

app:tabMode="fixed"

编辑:

我正在使用它并且它正在工作:

             <android.support.design.widget.TabLayout
                android:id="@+id/tab_layout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/toolbar"
                android:background="?attr/colorPrimary"
                android:minHeight="?attr/actionBarSize"
                android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />

好像不需要设置什么。

证明:

顺便说一句,你也可以检查这个:

tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);

像这样设置TabLayoutMode

tabLayout.setTabMode(TabLayout.MODE_FIXED);

解决方案是将这些添加到 xml 和 java 中:

在Java中:

tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);

并且在 XML 中用于 TabLayout:

            app:tabGravity="center"
            app:tabMode="fixed"

结果: