首页 > 解决方案 > 浮动操作按钮波纹不起作用

问题描述

我无法在浮动操作按钮上实现涟漪效果,我完全按照网上所说的内容进行操作,但我看不到涟漪效果

<com.google.android.material.floatingactionbutton.FloatingActionButton
            android:id="@+id/new_chat"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="16dp"
            android:layout_marginBottom="16dp"
            android:clickable="true"
            android:src="@drawable/add_chat"
            android:tooltipText="Start a new chat"
            app:fabSize="normal"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:maxImageSize="56dp"
            app:rippleColor="@color/pastel_red" />

标签: androidxmlfloating-action-button

解决方案


将此添加到您的styles.xml

<style name="SelectableItemTheme">
    <item name="colorControlHighlight">@color/pastel_red</item>
</style>

<style name="SelectableItemBackground">
    <item name="android:theme">@style/SelectableItemTheme</item>
    <item name="android:background">?attr/selectableItemBackgroundBorderless</item>  #this makes it round, if you want square, remove the word "Borderless"
</style>

接着

<com.google.android.material.floatingactionbutton.FloatingActionButton
            style="@style/SelectableItemBackground"
            android:id="@+id/new_chat"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="16dp"
            android:layout_marginBottom="16dp"
            android:clickable="true"
            android:src="@drawable/add_chat"
            android:tooltipText="Start a new chat"
            app:fabSize="normal"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:maxImageSize="56dp" />

推荐阅读