材料设计扩展的浮动动作按钮没有波纹效果

时间:2020-06-12 14:20:08

标签: android material-design floating-action-button material-components-android ripple-effect

我正在创建一个Android应用,并且想使用扩展的浮动操作按钮,因此我将以下代码添加到了活动中:

<com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton
        android:id="@+id/new_game_fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        android:layout_gravity="bottom|center"
        android:text="@string/main_new_game"
        android:backgroundTint="@color/colorAccent"
        app:icon="@drawable/ic_play_arrow_24px"/>

该按钮的外观应与预期完全一样,只是它在单击时不会产生连锁反应。

如何添加波纹效果?我直接从https://material.io/develop/android/components/floating-action-button/#extended-fabs那里获取了代码,看起来波纹应该默认存在,但是在我的应用中不起作用。我试图创建一个新项目,在该项目中我仅设置了材料组件(https://material.io/develop/android/docs/getting-started/),并且该按钮仍然没有波纹效果。因此,看来我的项目设置不是问题。

我还尝试设置app:rippleColor属性,将android:clickable="true" android:focusable="true"设置为无效。只有那种工作可以设置android:foreground="?attr/selectableItemBackground",但是波纹效果被屏蔽为矩形,而不是扩展FAB的形状。设置前景也不是一个很好的方法,因为只有API 23和更高版本支持它,而我的目标是API 21。

2 个答案:

答案 0 :(得分:1)

您应该使用此属性app:rippleColor

<com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton
        android:id="@+id/new_game_fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        android:layout_gravity="bottom|center"
        android:text="@string/main_new_game"
        android:backgroundTint="@color/colorAccent"
        app:icon="@drawable/ic_play_arrow_24px"
        app:rippleColor="@color/colorPrimary" />

答案 1 :(得分:1)

ExtendedFloatingActionButton的默认样式具有基于 rippleColor 默认 colorOnSecondary选择器。 将此颜色签入您的应用主题。

无论如何,您都可以使用:

<com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton
        app:rippleColor="@color/my_selector" />

或者您可以使用以下方法覆盖colorOnSecondary中的ExtendedFloatingActionButton

<com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton
        android:theme="style/ExFabOverlay" />

具有:

<style name="ExFabOverlay">
  <item name="colorOnSecondary">@color/my_color</item>
</style>

最后的注释:使用app:backgroundTint代替android:backgroundTint