【问题标题】:Custom styling of com.google.android.material.tabs.TabLayoutcom.google.android.material.tabs.TabLayout 的自定义样式
【发布时间】:2018-11-10 11:11:41
【问题描述】:

我正在为 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,但仍然无法正常工作!

我该如何解决这个问题?更改材质组件颜色/主题的正确方法是什么?

【问题讨论】:

    标签: android material-components-android


    【解决方案1】:

    通过这些更改,它可以按预期工作:

    <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>
    

    结果:

    【讨论】:

    • 我使用的是 android:theme 而不是 style,这就是导致问题的原因!
    【解决方案2】:

    到目前为止,您应该使用来自here 的 Widget.MaterialComponents.TabLayout.Colored。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-08-10
      • 2017-02-10
      • 2011-05-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多