How to set gradient drawable as backgroundTint att

2019-03-01 12:35发布

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>

2条回答
疯言疯语
2楼-- · 2019-03-01 13:14

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"

查看更多
老娘就宠你
3楼-- · 2019-03-01 13:26

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.

查看更多
登录 后发表回答