【问题标题】:How to change the fill color of the image of floating action button in android?如何更改android中浮动操作按钮图像的填充颜色?
【发布时间】:2022-01-08 11:03:23
【问题描述】:

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

浮动操作按钮的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 xml floating-action-button


    【解决方案1】:

    themes.xml 文件中(路径:app -> src -> main -> res -> values -> theme.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" />
    

    【讨论】:

    • 您不必更改主题的colorOnSecondary,而是在您的fab xml 代码中设置app:tint 属性。
    • 我没有看到 app:tint 而是 android:tint 。命名空间不同。
    • 感谢您的新答案它有效,我一定会在我的答案中添加它
    【解决方案2】:

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

    但也许你可以关注android:strokeColor

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

    您也许可以使用tintMode 来将效果/颜色仅应用于图像中的文本。

    【讨论】:

    • 我想改变浮动操作按钮图像的填充颜色
    • 是的,看看 tintModes 来尝试获得您想要的效果。因为它在技术上是一种图像资源,所以主题颜色和填充属性不适用于它。您可能需要结合使用 tintMode 和某种前景/背景色调。色调模式有时会影响特定布局内的非均匀颜色。
    猜你喜欢
    • 2015-09-07
    • 2018-12-11
    • 2015-08-07
    • 1970-01-01
    • 2017-11-13
    • 2022-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多