为什么我的 FloatingActionButton 图标是黑色的?

Why is my FloatingActionButton icon black?

以下是我正在使用的代码。我正在使用安卓x。每个 FAB 都有一个黑色图标,即使它是白色的。

mylayout.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <include layout="@layout/content_geral" />

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="24dp"
        app:backgroundTint="@color/colorPrimary"
        app:srcCompat="@drawable/ic_cloud_upload"
        tools:ignore="VectorDrawableCompat" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

style.xml

 <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="windowNoTitle">true</item>
        <item name="windowActionBar">false</item>
    </style>

您正在更改 FAB 的背景 颜色,而不是图标颜色。 要更改 图标 颜色,请使用:

android:tint

更新

您还可以通过编程方式更改颜色:

Drawable myFabSrc = getResources().getDrawable(android.R.drawable.ic_input_add);
Drawable willBeWhite = myFabSrc.getConstantState().newDrawable();
willBeWhite.mutate().setColorFilter(Color.WHITE, PorterDuff.Mode.MULTIPLY);
myFabName.setImageDrawable(willBeWhite);

您正在使用 android:backgroundTint 此 属性 设置 FAB 的背景颜色,但要更改 FAB 图标的颜色,请使用 android:tint 属性,如下所示:

    <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:tint="@android:color/white"
    android:src="@android:drawable/ic_input_add"
   />

在您的可绘制文件夹中点击

ic_cloud_upload

并将 fillColor 更改为

android:fillColor="#FFFFFF" // #FFFFFF is for white color

这会将您的黑色图标变为白色。

AndroidX 的 FloatingActionButton class 使用 colorOnSecondary 主题属性为其图标着色。

https://github.com/material-components/material-components-android/blob/master/lib/java/com/google/android/material/floatingactionbutton/res/values/styles.xml#L39

如果您遵循 MaterialComponents 主题定义进入基本定义,您会看到 colorOnSecondary 的默认值是 design_default_color_on_secondary... 和 定义为 #000000

https://github.com/material-components/material-components-android/blob/master/lib/java/com/google/android/material/color/res/values/colors.xml#L26

您可以通过将 app:tint 属性直接添加到您的 FloatingActionButton 或在您的主题中重新定义 @color/colorOnSecondary 来解决此问题。

如果您使用的是 AndroidX,要更改图标的颜色,您必须使用 app:tint 而不是 android:tint

<com.google.android.material.floatingactionbutton.FloatingActionButton
    style="@style/Widget.MaterialComponents.FloatingActionButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="16dp"
    android:layout_marginBottom="16dp"
    app:backgroundTint="@color/colorPrimary"
    app:tint="@color/white"
    app:srcCompat="@drawable/ic_add"
/>

我有一个有多种颜色的图标(矢量)(附件),但我不能使用 app:tint="@color/white" 因为我的图标的颜色变成单一颜色,比如白色,我不需要这个。

所以我解决了将设置 app:tint 设置为 null 的问题:

  app:tint="@null"

我的图标 (SVG) :