如何将FAB的背景设置为渐变 [英] How to set the background of a FAB as a gradient

查看:75
本文介绍了如何将FAB的背景设置为渐变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法使用渐变设置FAB的背景.FAB的背景显示为 black 颜色我的mainActivtiy xml文件在下面给出

I can't set the background of a FAB with a gradient. The background of the FAB shows a black color My mainActivtiy xml file is given below

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <com.google.android.material.bottomappbar.BottomAppBar
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:backgroundTint="@color/colorPrimary"
        app:menu="@menu/bottom_nav"/>

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/button_gradient"/>


</androidx.coordinatorlayout.widget.CoordinatorLayout>

我的自定义渐变文件如下

My custom gradient file is as follows

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="oval">
            <gradient
                android:angle="90"
                android:startColor="#E12160"
                android:endColor="#3F5CC8" />
            <size
                android:width="56dp"
                android:height="56dp" />
        </shape>
    </item>
    <item
        android:left="10dp"
        android:right="10dp">
        <shape android:shape="line">
            <stroke
                android:width="2dp"
                android:color="#ffffff" />
        </shape>
    </item>
    <item
        android:left="10dp"
        android:right="10dp">
        <rotate
            android:fromDegrees="90">
            <shape android:shape="line">
                <stroke
                    android:width="2dp"
                    android:color="#ffffff" />
            </shape>
        </rotate>
    </item>
</layer-list>

我得到的输出如下

我的渐变xml如下所示

My gradient xml looks like below

推荐答案

使用 foreground 属性将可绘制对象呈现在视图中所有内容之上

use the foreground attribute to render your drawable above all of the content in the view

<com.google.android.material.floatingactionbutton.FloatingActionButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:foreground="@drawable/button_gradient"/>

这篇关于如何将FAB的背景设置为渐变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆