我正在创建一个Android应用程序,我想使用扩展的浮动动作按钮,所以我添加了以下代码到我的activity:
<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/developer/android/components/floating-action-button/#extended-fabs中获取了代码,看起来ripple在默认情况下应该存在,但它在我的应用程序中不起作用。 我曾尝试创建一个新项目,其中我只设置了材料组件(https://Material.io/developer/android/docs/gett-started/),按钮仍然没有涟漪效应。 所以它似乎不是一个问题与我的项目设置。
我还尝试过设置app:rippleColor
属性,设置android:clickable=“true”android:focusable=“true”
没有效果。 唯一起作用的是设置android:foreground=“?attr/selectableitembackground”
,但是涟漪效果被屏蔽为矩形,而不是扩展晶圆厂的形状。 另外,设置前景也不是很好的方法,因为它只支持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" />