我正在创建一个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。
您应该使用此属性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" />
ExtendedFloatingActionButton
的默认样式具有基于rippleColor