如果您在XML中使用它,它可以工作,但您不能以编程方式设置它。我正在使用这个库:
implementation 'com.google.android.material:material:1.0.0'
例如:
filterHistoryFab.backgroundTintList = ContextCompat.getColorStateList(context!!, R.color.pink)
XML:
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/filterHistoryFab"
style="@style/FloatingActionButton"
android:visibility="gone"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:srcCompat="@drawable/ic_filter_white" />
样式:
<style name="FloatingActionButton">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:backgroundTint">@color/dark_blue</item>
<item name="android:layout_margin">@dimen/fab_margin</item>
<item name="android:background">?attr/selectableItemBackgroundBorderless</item>
</style>
背景应该是PINK,但它保持蓝色,后面有一些粉红色。链接到它的外观:LINK
我想到了。如果你设置在STYLE backgroundTint
它将无法以编程方式工作。也许这是一个错误。所以解决方案就是删除这一行:
<item name="android:backgroundTint">@color/dark_blue</item>
而且效果很好。