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

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

enter image description here

然而,我希望 FAB 图标看起来像以下图片:

enter image description here

我的 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的值,但它没有影响内部图标样式。
谢谢任何帮助。
5个回答

4
从图像资源导入与添加图标相对应的剪贴画有效:
步骤:
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

enter image description here

在此输入图片描述 为了将其用作图标,请在您的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" />

2
尝试下载并使用名称为“add”的图标,而不是“add circle”。

无论如何,谢谢你,你的回答帮助我找到了解决方案。请查看我的答案以获取完整的解决方案。感谢帮助 :) - Shubham Khatri
谢谢 :) @ShubhamKhatri - Rashmi Mathur

2
这对我有用。
<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图标应该是相同的。

我使用了xhdpi drawable的140x140


1

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


无论如何,谢谢你,你的答案让我朝着正确的方向前进。请查看我的答案以获取完整的解决方案。+1 帮助 :) - Shubham Khatri

0

你需要设置scaleType以使其正确渲染:

 android:scaleType="center"

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