如何更改android中浮动操作按钮图像的填充颜色?

How to change the fill color of the image of floating action button in android?

我正在学习 android 并且想知道有什么方法可以将 FloatingActionButton 的填充颜色从黑色更改为白色。我使用的矢量资源也是白色填充颜色,但填充颜色仍然是黑色。我已经阅读了很多关于 FloatingActionButton 的文章和帖子,但其中 none 帮助我解决了我的问题。

浮动操作按钮的 xml 代码

<com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/floatingActionButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="30dp"
        android:layout_marginBottom="30dp"
        android:clickable="true"
        android:tint="#ffffff"
        app:backgroundTint="#3F51B5"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:rippleColor="#ffffff"
        app:srcCompat="@drawable/ic_baseline_add_24"
        android:focusable="true"
        tools:ignore="VectorDrawableCompat" />

矢量资产

<vector android:alpha="0.74" android:height="24dp"
    android:tint="#FFFFFF" android:viewportHeight="24"
    android:viewportWidth="24" android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
    <path android:fillColor="@android:color/white" android:pathData="M19,13h-6v6h-2v-6H5v-2h6V5h2v6h6v2z"/>
</vector>

提前致谢

嗯嗯,从技术上讲,矢量是一种绘图,所以不确定文本颜色是什么意思。

但也许你会在 android:strokeColor 之后?

但是看起来你在按钮上分配了一个可绘制对象,文本是绘图的一部分吗?如果不设置,我们可能无法正确回答您的问题。

您可以尝试使用 tintMode 将 affect/color 仅应用于图像中的文本。

themes.xml 文件中(路径:app -> src -> main -> res -> values -> themes.xml)将 colorOnSecondary 项的值从@color/black@color/white 或您选择的任何颜色,但请确保颜色应在您的 colors.xml 中,否则在您的 themes.xml 中添加十六进制颜色代码。

themes.xml 文件将如下所示

<resources xmlns:tools="http://schemas.android.com/tools">
    <!-- Base application theme. -->
    <style name="Theme.SmartNotes" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
        <!-- Primary brand color. -->
        <item name="colorPrimary">@color/primaryColor</item>
        <item name="colorPrimaryVariant">@color/purple_700</item>
        <item name="colorOnPrimary">@color/white</item>
        <!-- Secondary brand color. -->
        <item name="colorSecondary">@color/teal_200</item>
        <item name="colorSecondaryVariant">@color/teal_700</item>
        <item name="colorOnSecondary">@color/white</item>
        <!-- Status bar color. -->
        <item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
        <!-- Customize your theme here. -->
    </style>
</resources>

或在浮动操作按钮的 xml 代码中添加 app:tint,如下所示

<com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/floatingActionButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="30dp"
        android:layout_marginBottom="30dp"
        android:clickable="true"
        app:tint="#ffffff"
        app:backgroundTint="#3F51B5"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:rippleColor="#ffffff"
        app:srcCompat="@drawable/ic_baseline_add_24"
        android:focusable="true"
        tools:ignore="VectorDrawableCompat" />