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

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

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

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


当前回答

我是这样做的 android:背景= " @color / colorAccent” 我只需要到文件夹res,然后点击文件夹values,然后在colors。xml中点击colors。xml,我只需要改变colorAccent的颜色并在android:background中调用它,它就完成了

其他回答

如果您希望以编程方式更改颜色,则可以使用此代码

floating.setBackgroundTintList(getResources().getColorStateList(R.color.vermelho));

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

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

并在代码中设置颜色:

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

正如Vasil Valchev在评论中指出的那样,它比看起来要简单,但是在我的XML中有一个我没有注意到的细微差别。

<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"
    app:backgroundTint="@android:color/white"/>

注意它是:

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

而不是

android:backgroundTint="@android:color/white"

我是这样做的 android:背景= " @color / colorAccent” 我只需要到文件夹res,然后点击文件夹values,然后在colors。xml中点击colors。xml,我只需要改变colorAccent的颜色并在android:background中调用它,它就完成了

我也有同样的问题,都在抓我的头发。谢谢你 https://stackoverflow.com/a/35697105/5228412

我们能做什么…

 favourite_fab.setImageDrawable(ContextCompat.getDrawable(getBaseContext(), R.drawable.favourite_selected));

这对我来说很好,希望其他人也能到达这里。