选项卡图标未填满全宽

tab icons not filling full width

我尝试了几种解决方案,但没有任何效果。在某些设备上,选项卡栏不会填满父项的宽度。它在 Samsung Tab 2 等设备上运行良好,但在 Pixel 2 等设备上不起作用(如附图)。

<android.support.design.widget.TabLayout
        android:id="@+id/tabLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabTextAppearance="@style/MyTabLayoutTextAppearance"
        app:layout_constraintBottom_toBottomOf="parent"
        app:tabIndicatorColor="@color/colorBackground"
        app:tabMode="scrollable"
        app:tabMaxWidth="0dp"
        app:tabGravity="fill"
        app:tabSelectedTextColor="@color/colorPrimary"
        />

`

使用fixed tabMode 而不是scrollable

app:tabMode="fixed"

您还必须使用 app:tabMode="fixed" 属性。
类似于:

  <com.google.android.material.tabs.TabLayout
      app:tabGravity="fill"
      app:tabMode="fixed"
      ..>

同时检查 doc: 在支持设计库中:

Gravity used to fill the TabLayout as much as possible. This option only takes effect when used with MODE_FIXED.

Material Components Library:

Gravity used to fill the TabLayout as much as possible. This option only takes effect when used with MODE_FIXED on non-landscape screens less than 600dp wide.