我一直试图改变材质的浮动动作按钮的颜色,但没有成功。

<android.support.design.widget.FloatingActionButton
    android:id="@+id/profile_edit_fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="end|bottom"
    android:layout_margin="16dp"
    android:clickable="true"
    android:src="@drawable/ic_mode_edit_white_24dp" />

我试着补充:

android:background="@color/mycolor"

或者通过代码:

FloatingActionButton fab = (FloatingActionButton) rootView.findViewById(R.id.profile_edit_fab);
fab.setBackgroundColor(Color.parseColor("#mycolor"));

or

fab.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#mycolor")));

但上述方法都不起作用。我也尝试了提出的重复问题的解决方案,但没有一个可行;按钮仍然是绿色的,也变成了一个正方形。

附:如果知道如何增加连锁反应也很好,我也不明白。


当前回答

Vijet Badigannavar的答案是正确的,但使用ColorStateList通常是复杂的,他没有告诉我们如何做到这一点。由于我们经常关注在正常和按下状态下改变视图的颜色,我将添加更多细节:

If you want to change FAB's color in normal state, you can just write mFab.setBackgroundTintList(ColorStateList.valueOf(your color in int)); If you want to change FAB's color in pressed state, thanks for Design Support Library 22.2.1, you can just write mFab.setRippleColor(your color in int); By setting this attribute, when you long-pressed the FAB, a ripple with your color will appear at your touch point and reveal into whole surface of FAB. Please notice that it won't change FAB's color in normal state. Below API 21(Lollipop), there is no ripple effect but FAB's color will still change when you're pressing it.

最后,如果你想为状态实现更复杂的效果,那么你应该深入研究ColorStateList,这里有一个SO问题讨论它:我如何以编程方式创建ColorStateList ?

更新: 谢谢@Kaitlyn的评论。要删除FAB的笔画使用backgroundTint作为它的颜色,你可以设置app:borderWidth="0dp"在你的xml。

其他回答

如文档中所述,默认情况下,它接受styles.xml属性colorAccent中的颜色集。

此视图的背景颜色默认为主题的colorAccent。如果你想在运行时改变这个,那么你可以通过setBackgroundTintList(ColorStateList)这样做。

如果你想改变颜色

在XML中带有属性app:backgroundTint

<android.support.design.widget.FloatingActionButton
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_add"
    app:backgroundTint="@color/orange"
    app:borderWidth="0dp"
    app:elevation="6dp"
    app:fabSize="normal" >

在代码中用.setBackgroundTintList(答案由ywwynm提供)

正如@Dantalian在评论中提到的,如果你想改变设计支持库v22(包括)的图标颜色,你可以使用

android:tint="@color/white"     

对于自v23以来的设计支持库,您可以使用:

app:tint="@color/white"   

androidX库也需要在你的xml布局中设置一个0dp边界:

<com.google.android.material.floatingactionbutton.FloatingActionButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_add"
    app:backgroundTint="@color/orange"
    app:borderWidth="0dp"
    app:elevation="6dp"
    app:fabSize="normal" />

使用下面的线条改变浮动动作按钮的背景颜色

app:backgroundTint="@color/blue"

改变浮动动作按钮图标的颜色

android:tint="@color/white"     

材质1.1.0中浮动动作按钮的新主题属性映射

在你的应用主题中:

设置colorSecondary为FAB设置背景颜色(映射到backgroundTint) 设置colorOnSecondary为图标/文本和FAB的波纹色设置颜色(映射到tint和rippleColor)


<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
    <!-- ...whatever else you declare in your app theme.. -->
    <!-- Set colorSecondary to change background of FAB (backgroundTint) -->
    <item name="colorSecondary">@color/colorSecondary</item>
    <!-- Customize colorSecondary to change icon/text of FAB (maps to tint and rippleColor) -->
    <item name="colorOnSecondary">@android:color/white</item>
</style>

感谢自动完成功能。经过几次尝试,我很幸运:

    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    card_view:backgroundTint="@color/whicheverColorYouLike"

——或——(两者基本上是一样的)

    xmlns:app="http://schemas.android.com/apk/res-auto"
    app:backgroundTint="@color/whicheverColorYouLike"

这在API Version 17和设计库23.1.0上为我工作。

对于材质设计,我只是改变了浮动动作按钮的颜色,就像这样, 在浮动动作按钮xml中添加以下两行。 和完成,

 android:backgroundTint="@color/colorPrimaryDark"
 app:borderWidth="0dp"