android 中的浮动操作按钮显示不正确

Floating action button in android not displaying correctly

我正在尝试将默认 FloatingActionButton 图标更改为 add circle 图标。我已经从 material.io 下载了 icon ic_add_circle_white_18dp 并将其添加到 andorid 项目中的 drawable directory 中。但是,当我尝试在我的 activity 中使用它时,它显示为黑色内部图标,如图像

但是我希望 FAB 图标看起来像

我的 FAB 图标 xml 看起来像

<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:layout_margin="@dimen/fab_margin"
        app:srcCompat="@drawable/ic_add_fab"
        app:elevation="8dp"
        app:backgroundTint="@android:color/holo_blue_light"
        app:rippleColor="@android:color/white"/> 

我已经尝试设置 app:backgroundTint 值,但它不会影响内部图标样式

感谢您的帮助

尝试下载并使用名称为 "add" 的图标,而不是 "add circle"

而不是 app:srcCompat= 对图像使用 app:src=
或使用fab.setImageResource(R.drawable.ic_add);以编程方式设置图像。

正在从图像资源作品中导入与添加图标对应的剪贴画:

步骤:

1. Right Click on res.
2. Select New->Image Asset
3. In Icon type, select Action Bar and Tab Icons
4. Provide a name
5. Select ClipArt in Asset type and the click on the Clip Art icon.
6. From the content section select add button
7. In theme set HOLO_DARK
6. Click Next and then finish

为了将其用作图标,请在 XML 中包含以下内容。请注意,使用图标作为来源 android:src="@drawable/ic_add_fab" 被使用

<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:layout_margin="@dimen/fab_margin"
        android:src="@drawable/ic_add_fab" />

您需要设置 scaleType 才能正确呈现:

 android:scaleType="center"

这对我有用

<android.support.design.widget.FloatingActionButton
                   android:id="@+id/logout_btn"
                   android:layout_width="wrap_content"
                   android:layout_height="wrap_content"
                   android:src="@mipmap/logout"
                   app:rippleColor="@null"
                   app:backgroundTint="@null"
                   app:fabSize="normal"
                   android:layout_gravity="bottom|center_horizontal"
                   android:scaleType="center"/>

另外src icon也要根据要求保持一致

我用了140x140 for xhdpi drawable