TabLayout highlite和Ripple效果

Tib*_*ral 1 android android-tablayout

我对TabLayout有两个问题

1)我可以删除TabLayout突出显示或更改高亮颜色吗?

2)我可以为标签添加涟漪效果.每个选项卡都包含TextView我尝试添加这样的自定义背景

<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="?android:colorControlHighlight">
    <item android:drawable="@drawable/btn_white_bg" />
</ripple>
Run Code Online (Sandbox Code Playgroud)

但它不起作用.

小智 9

要删除突出显示,请将以下行添加到您的 XML:

app:tabRippleColor="@android:color/transparent"
Run Code Online (Sandbox Code Playgroud)


Mun*_*oor 7

另一个适合我的解决方案

<android.support.design.widget.TabLayout
    android:id="@+id/tab_layout"
    android:minHeight="?attr/actionBarSize"
    android:layout_width="match_parent"
    app:tabMode="fixed"
    app:tabGravity="fill"
    android:clipToPadding="false"
    android:elevation="0dp"
    style="@style/MyCustomTabLayout"
    android:background="@color/colorPrimary"
    app:tabBackground="?attr/selectableItemBackground"
    app:tabIndicatorColor="@color/app_yellow"
    app:tabIndicatorHeight="4dip"
    android:layout_height="wrap_content"/>
Run Code Online (Sandbox Code Playgroud)

我刚刚添加了以下几行

机器人:背景= "@颜色/ colorPrimary"

应用:tabBackground = "?ATTR/selectableItemBackground"


小智 3

您可以像这样自定义 TabLayout:在值中创建一个 xml 文件MyCustomTabLayout.xml,然后将这些值放入

<resources>

<style name="MyCustomTabLayout" parent="Widget.Design.TabLayout">
    <item name="tabMaxWidth">@dimen/tab_max_width</item>
     <item name="tabIndicatorColor">@color/black</item>
   <!--  <item name="tabIndicatorColor">?attr/colorAccent</item> -->
    <item name="tabIndicatorHeight">5dp</item>
    <item name="tabPaddingStart">12dp</item>
    <item name="tabPaddingEnd">12dp</item>
    <item name="tabBackground">?attr/selectableItemBackground</item>
    <item name="tabTextAppearance">@style/MyCustomTabTextAppearance</item>
    <item name="tabSelectedTextColor">?android:textColorPrimary</item>
</style>

<style name="MyCustomTabTextAppearance" parent="TextAppearance.Design.Tab">
    <item name="android:textSize">16sp</item>
    <item name="android:textColor">?android:textColorSecondary</item>
    <item name="textAllCaps">true</item>
</style>
Run Code Online (Sandbox Code Playgroud)

并在你的布局中添加以下内容:

<android.support.design.widget.TabLayout
        android:id="@+id/mainSlidingTab"
        style="@style/MyCustomTabLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/tool_bar"
        android:background="@color/ColorPrimary" />
    <!-- app:tabMode="scrollable" when many tabs -->
Run Code Online (Sandbox Code Playgroud)