如何在 xml 文件中或以编程方式更改浮动操作按钮的颜色

How to change the color of floating action button in xml file or programmatically

我是android世界的新手。为了提高自己的技能,我正在学习android。所以我正在创建一个社交应用程序,我使用了浮动操作按钮,如果用户点击它会增加喜欢的数量。

默认情况下,浮动按钮采用强调色,但我想使用不同的颜色。我尝试了很多但没有得到任何解决方案。是的,我可以使用很多库来实现我的目标,但我不想使用任何外部库。我相信使用 android.

的核心方法

难道除了使用外部库没有解决方案吗?我试过这个,但是xml文件在我使用时抛出错误应用标签。

If you wish to change the color

    in XML with attribute app:backgroundTint
    in code with .setBackgroundTintList

我正在使用这个库:

com.android.support:design:23.1.1

请发表评论。我知道对于 Android 大师来说这没什么大不了的。我知道这个问题已经存在,但我没有得到解决我的问题的方法。这就是为什么我再次打开这个问题。

如文档中所述,默认情况下它采用 styles.xml 属性 colorAccent 中设置的颜色。

The background color of this view defaults to the your theme's colorAccent. If you wish to change this at runtime then you can do so via setBackgroundTintList(ColorStateList).

如果你想改变颜色

在 XML 中,属性为 app:backgroundTint

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

在代码中使用 setBackgroundTintList