How to set gradient drawable as backgroundTint attribute on FloatingActionButton

▼魔方 西西 提交于 2019-12-02 13:12:05

问题


I would like to use a gradient color instead of the traditional solid color for a Floating Action Button.

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>

回答1:


Yes, you can set a Gradient to a FAB, I've just found a heck.

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.) In your dimens.xml add this piece of line,

<dimen name="design_fab_image_size" tools:override="true">56dp</dimen>

3.) In your Fab add android:src="@drawable/gradient"




回答2:


No. As you already noticed, tint is always a solid color. You can set anything as a background, but I guess such approach would break the support FAB.



来源:https://stackoverflow.com/questions/43700146/how-to-set-gradient-drawable-as-backgroundtint-attribute-on-floatingactionbutton

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!