com.google.android.material.tabs.TabLayout 的自定义样式

Custom styling of com.google.android.material.tabs.TabLayout

我正在为 Android 使用新的 Google Material Components

目前,我正在尝试为 Tablayout (com.google.android.material.tabs.TabLayout) 提供自定义外观。

我创建的样式如下:

<style name="AppTheme.TabLayout" parent="Widget.MaterialComponents.TabLayout">
    <item name="android:background">@color/colorPrimary</item>
</style>

但是,应用此主题后,选项卡指示器变得不可见

无主题:

主题:

此外,我尝试了其他方法,例如更改 tabIndicatorColor,但仍然无效!

我该如何解决这个问题?更改 Material 组件的 colors/theme 的正确方法是什么?

经过这些更改,它按预期工作:

<com.google.android.material.tabs.TabLayout
     android:id="@+id/tabLayout"
     style="@style/AppTheme.TabLayout"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     app:layout_collapseMode="pin"
     app:tabGravity="fill"
     app:tabTextAppearance="@style/customFontStyle">

     //Tab items maybe

</com.google.android.material.tabs.TabLayout>

Styles.xml:

<style name="AppTheme.TabLayout" parent="Widget.MaterialComponents.TabLayout">
    <item name="android:background">@color/colorPrimary</item>
    <item name="tabIndicatorColor">@color/yourcolor</item>
</style>

结果:

截至目前,您应该使用 here 中的 Widget.MaterialComponents.TabLayout.Colored。