如何设置 TabLayout ta 的圆形波纹形状?

How can I set round ripple shape of TabLayout's ta?

我要设置 tablayout 选项卡的圆形波纹形状。 TabLayout 链接到 viewpager。我尝试定义选择器 XML 并使用选择器 xml 设置 tablayout 的 tabbackground 属性。 所以它已经被设置了。但问题是它在我先点击它之后才起作用。所以这意味着每个选项卡的涟漪在第一次点击时都不起作用。什么原因? 谢谢

这是我的代码。

<com.google.android.material.tabs.TabLayout
        android:id="@+id/unit_categories"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginEnd="16dp"
        android:theme="@style/Theme.AppCompat.Light.NoActionBar"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:tabBackground="@drawable/unit_category_selector_tab_background"
        app:tabGravity="center"
        app:tabIndicator="@drawable/unit_category_tab_indicator"
        app:tabIndicatorFullWidth="true"
        app:tabIndicatorGravity="center"
        app:tabIndicatorHeight="40dp"
        app:tabMode="scrollable"
        app:tabRippleColor="@android:color/transparent"
        app:tabUnboundedRipple="false" />

这是unit_category_selector_tab_background.xml

<?xml version="1.0" encoding="utf-8"?>
<item android:state_pressed="true" android:state_selected="false" android:drawable="@drawable/unit_category_tab_ripple"/>
</selector>

这是unit_category_tab_ripple.xml

<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="#9BB3B1">
    <item>
        <shape
            android:shape="rectangle"
            >
            <solid android:color="#FFFEFE" />
            <corners
                android:radius="40dp" />
        </shape>
    </item>
</ripple>
<com.google.android.material.tabs.TabLayout
android:id="@+id/unit_categories"
android:layout_width="match_parent"
android:layout_height="@dimen/unit_category_tab_height"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:background="@android:color/transparent"
android:theme="@style/Theme.AppCompat.Light.NoActionBar"
app:tabGravity="center"
app:tabIndicator="@drawable/unit_category_tab_indicator"
app:tabIndicatorFullWidth="true"
app:tabIndicatorGravity="center"
app:tabIndicatorHeight="@dimen/unit_category_tab_height"
app:tabMode="scrollable"
app:tabRippleColor="@android:color/transparent"
app:tabTextColor="@color/unit_calc_text_color"
app:tabUnboundedRipple="false" />

tab_indicator.xml

<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="#D584D5">
<item>
    <shape
        android:shape="rectangle">
        <!-- radius should be half of the desired TabLayout height -->
        <corners
            android:radius="@dimen/unit_category_tab_height"/>
        <!-- color of the selected tab -->
        <solid android:color="@android:color/transparent"/>
        <stroke android:width="1dp" android:color="@color/tab_indicator_color"/>
    </shape>
</item>
</ripple>

tab_background.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android" android:enterFadeDuration="0" android:exitFadeDuration="1500">
    <item android:state_pressed="true" android:state_selected="false" android:drawable="@drawable/unit_category_tab_ripple"/>
</selector>

我是在tab变化的时候通过代码设置背景的

tab_riple.xml

<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="#C1DCD9">
    <item>
        <shape android:shape="rectangle">
            <solid android:color="@color/calculator_primary_color" />
            <corners android:radius="@dimen/unit_category_tab_height" />
            <stroke android:width="2dp" android:color="@android:color/transparent"/>
        </shape>
    </item>
</ripple>