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

<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")));

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

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


当前回答

我们忽略的一点是,在你设置按钮的颜色之前,重要的是要为这个颜色设置你想要的值。你可以到values > color。您将找到默认的颜色,但您也可以通过复制和粘贴它们来创建颜色,更改颜色和名称。然后……当您要更改浮动按钮(在activity_main中)的颜色时,您可以选择已经创建的那个按钮

示例-代码的值>颜色默认颜色+ 3个我已经创建的颜色:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#3F51B5</color>
    <color name="colorPrimaryDark">#303F9F</color>
    <color name="colorAccent">#FF4081</color>

    <color name="corBotaoFoto">#f52411</color>
    <color name="corPar">#8e8f93</color>
    <color name="corImpar">#494848</color>

</resources>

现在我的浮动动作按钮,颜色我已经创建并命名为corPar:

<android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        android:src="@android:drawable/ic_input_add"
        android:tint="#ffffff"
        app:backgroundTint="@color/corPar"/>

这对我很管用。好运!

其他回答

我的解决方案,用数据绑定对我有效

val color = ContextCompat.getColor(context, R.color.colorPrimary)
binding.fab.backgroundTintList = ColorStateList.valueOf(getColor)

其他解决方案也可能奏效。这是10磅大猩猩的方法,其优点是广泛适用于这种情况和类似情况:

Styles.xml:

<style name="AppTheme.FloatingAccentButtonOverlay" >
    <item name="colorAccent">@color/colorFloatingActionBarAccent</item>
</style>

你的布局xml:

<android.support.design.widget.FloatingActionButton
       android:theme="AppTheme.FloatingAccentButtonOverlay"
       ...
 </android.support.design.widget.FloatingActionButton>

使用材质主题和材质组件FloatingActionButton,默认情况下,它采用styles.xml属性colorSecondary中的颜色集。

你可以在xml中使用app:backgroundTint属性:

<com.google.android.material.floatingactionbutton.FloatingActionButton
       ...
       app:backgroundTint=".."
       app:srcCompat="@drawable/ic_plus_24"/>

你可以使用fab.setBackgroundTintList(); 您可以使用<item name="backgroundTint">属性自定义样式

  <!--<item name="floatingActionButtonStyle">@style/Widget.MaterialComponents.FloatingActionButton</item> -->
  <style name="MyFloatingActionButton" parent="@style/Widget.MaterialComponents.FloatingActionButton">
    <item name="backgroundTint">#00f</item>
    <!-- color used by the icon -->
    <item name="tint">@color/...</item>
  </style>

从材质组件的1.1.0版本开始,你可以使用新的materialThemeOverlay属性来覆盖某些组件的默认颜色:

  <style name="MyFloatingActionButton" parent="@style/Widget.MaterialComponents.FloatingActionButton">
    <item name="materialThemeOverlay">@style/MyFabOverlay</item>
  </style>

  <style name="MyFabOverlay">
    <item name="colorSecondary">@color/custom2</item>
    <!-- color used by the icon -->
    <item name="colorOnSecondary">@color/...</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上为我工作。

如果你尝试用app改变FAB的颜色,会有一些问题。 按钮的边框有不同的颜色,所以你必须做什么:

app:backgroundTint="@android:color/transparent"

并在代码中设置颜色:

actionButton.setBackgroundTintList(ColorStateList.valueOf(getResources().getColor(R.color.white)));