安卓设计库TabLayout背景问题

4
设置 "22.2.0 Android设计库" TabLayout(android.support.design.widget.TabLayout)的 tabBackground 属性时,出现了两个问题:
  • 选项卡中的涟漪效果消失了
  • 选项卡指示器消失了。
这在Lollipop和Kitkat设备上都会出现。
如果不设置tabBackground,涟漪效果和选项卡指示器都可以工作,但是背景有一个与工具栏不同的默认颜色,这不符合材料设计指南的要求。
请查看以下XML:
 <android.support.design.widget.TabLayout
        android:id="@+id/tabLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabMode="fixed"
        app:tabGravity="fill"
        app:tabBackground = "?attr/colorPrimary" />
1个回答

16

使用android:background="?attr/colorPrimary"代替app:tabBackground = "?attr/colorPrimary"

如果您使用深色主题颜色,您可能还想将主题更改为ThemeOverlay.AppCompat.Dark。这将使文本和涟漪颜色变为白色。

完整示例:

<android.support.design.widget.TabLayout
        android:id="@+id/tabLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabMode="fixed"
        app:tabGravity="fill"
        android:background="?attr/colorPrimary"
        android:theme="@style/ThemeOverlay.AppCompat.Dark" />

太好了,现在它可以工作了,除了在KitKat设备上没有涟漪效果。你知道Android设计库TabLayout中的涟漪效果是否应该被移植到KitKat吗?谢谢。 - u2gilles
据我所知,没有这样的功能。我认为涟漪效果只在设计库中的5.0及以上版本中有效。 - Huby
android:background="@color/someColor" 这段代码可以设置背景颜色。如果有人想知道,还可以使用 android:theme="@style/ThemeOverlay.AppCompat.Dark" - ashishmohite

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接