如何在FloatingActionButton上将可绘制渐变设置为backgroundTint属性 [英] How to set gradient drawable as backgroundTint attribute on FloatingActionButton
问题描述
我想为浮动操作按钮使用渐变色而不是传统的纯色.
I would like to use a gradient color instead of the traditional solid color for a Floating Action Button.
我已经知道如何使用ColorStateList
修改按钮的背景颜色.有没有一种方法可以将以下可绘制对象实现为背景色?
I already know how to modify the background color of the button, using ColorStateList
. Is there a way to implement the following drawable as a background tint color?
overlay.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="#c25f82"
android:endColor="#5a3e60"
android:angle="-270" />
</shape>
推荐答案
是的,您可以为FAB设置渐变,我刚刚发现了一个问题.
Yes, you can set a Gradient to a FAB, I've just found a heck.
1.)在 drawable 文件夹中创建 gradient.xml ,并在其中添加以下代码,
1.) Create a gradient.xml in drawable folder and add the following code in it,
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<gradient
android:type="linear"
android:angle="0"
android:startColor="#03a9f4"
android:endColor="#e91e63" />
</shape>
2.)在您的 dimens.xml 中添加这一行,
2.) In your dimens.xml add this piece of line,
<dimen name="design_fab_image_size" tools:override="true">56dp</dimen>
3.)在您的Fab中添加android:src="@drawable/gradient"
3.) In your Fab add android:src="@drawable/gradient"
这篇关于如何在FloatingActionButton上将可绘制渐变设置为backgroundTint属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!