【问题标题】:How to set gradient drawable as backgroundTint attribute on FloatingActionButton如何将渐变可绘制设置为 FloatingActionButton 上的 backgroundTint 属性
【发布时间】:2017-09-27 17:59:12
【问题描述】:

我想为浮动操作按钮使用渐变色而不是传统的纯色。

我已经知道如何修改按钮的背景颜色,使用ColorStateList。有没有办法将以下可绘制对象实现为背景色调?

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>

【问题讨论】:

    标签: android drawable gradient background-color floating-action-button


    【解决方案1】:

    是的,您可以将渐变设置为 FAB,我刚刚发现了一个问题。

    1.) 在drawable文件夹中创建一个gradient.xml,并在其中添加以下代码,

        <?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 中添加这条线,

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

    3.) 在您的 Fab 中添加 android:src="@drawable/gradient"

    【讨论】:

      【解决方案2】:

      没有。正如您已经注意到的,色调始终是纯色。您可以将任何东西设置为背景,但我想这种方法会破坏支持 FAB。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-07-30
        • 1970-01-01
        • 1970-01-01
        • 2012-02-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多