设置FloatingActionButton的src和background

7
当我在 android.support.design.FloatingActionButton 中使用 background 和 src 时,它没有正确设置。相反,它被显示为:

enter image description here

<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/pink"
android:src="@drawable/ic_action_barcode_2"
android:layout_gravity="bottom|right"
android:layout_marginBottom="16dp"
android:layout_marginRight="16dp" />

但是当我使用 ImageView 时,它会正确显示为

enter image description here

<ImageView
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/pink"
android:src="@drawable/ic_action_barcode_2"
android:layout_gravity="bottom|right"
android:layout_marginBottom="16dp"
android:layout_marginRight="16dp" />

为什么FloatingActionButton显示不正确?我应该在我的代码中做出哪些改变?
2个回答

9
浮动操作按钮的背景不需要改变,你只需应用色调,然后像往常一样添加图标即可。
<android.support.design.widget.FloatingActionButton
    ...
    app:backgroundTint="@color/ic_action_barcode_2"
    android:src="@drawable/ic_add" />

这将为您提供一个圆形按钮,但颜色可以自行选择。

在此示例中,app 命名空间用于支持库功能:

xmlns:app="http://schemas.android.com/apk/res-auto"


3
谢谢你指出应用程序命名空间,挽救了我掉头发的危机。 - Jannie Theunissen

0

我遇到了类似的问题。我尝试设置FloatingActionButton的src或background,但无法通过src填充按钮。以下代码解决了我的问题。

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    .
    .
    .
    android:background="@drawable/round_icon"
    android:backgroundTintMode="src_over">

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接