如何更改浮动操作按钮的阴影颜色

5
我希望将浮动操作按钮的阴影颜色从黑色/灰色更改为colorprimary/自定义阴影颜色,如下图所示的中央蓝色FAB按钮的浅蓝色阴影,而不是灰色阴影。但我们可以更改FAB按钮的背景颜色。但是,正如您在图像中看到的那样,有FAB按钮的蓝色阴影。我希望实现这件事情。

enter image description here


请提供更多信息并编写XML和代码。 - Ahmad Aghazadeh
@ahmad aghazadeh 看,我已经更新了我的代码并附上了图像。请注意,在屏幕中心底部的蓝色FAB按钮具有浅蓝色阴影而不是灰色。我想要实现这个效果。 - Anant Shah
目前还没有编写任何 XML 代码。假设有一个带有浮动操作按钮的正常布局。需要更改屏幕时,需要更改 FAB 按钮阴影颜色属性。 - Anant Shah
5个回答

2

尝试这样做 :: app:backgroundTint="@color/colorAccentGrey"

其中colorAccentGrey = YourColor

如果忘记了,请在XML开头放置xmlns:app="http://schemas.android.com/apk/res-auto"

要去除阴影,请使用:app:elevation="0dp"

希望这能帮到您.. :)


2
不要这样做,这只会改变FAB按钮的背景颜色而不是阴影颜色。 - Anant Shah

0

0

0

没有一个答案对我有用。所以我自己写了这个。无论如何它会产生阴影效果,这对我来说已经足够了。

<com.google.android.material.floatingactionbutton.FloatingActionButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_add_black"
        app:elevation="0dp" // disable outside shawdow
        app:borderWidth="30dp" // make borderwidth to 25dp or height of fab
        app:backgroundTint="#00E5FF" // now you will see only this color with shawdow 
        android:backgroundTint="#00E5FF" // since border is 30dp u ll not see this color and if you want to check reduce border width to 25dp then ull see this color in center as a small dot.
        />

0

虽然晚了一些,但我找到了解决方案。让我们开始吧:

  1. res/drawable文件夹中创建一个名为shadow_bg的自定义背景形状,并将以下代码粘贴到其中:
<?xml version="1.0" encoding="utf-8"?>
    <shape android:shape="oval" 
     xmlns:android="http://schemas.android.com/apk/res/android">
    <gradient android:type="radial"
        android:startColor="@color/black"
        android:endColor="@android:color/transparent"
        android:gradientRadius="50dp"/>
    </shape>

startColor 是阴影的颜色,我这里使用黑色。

  1. 将您的 Activity 父布局设置为 CoordinatorLayout,并创建一个 FAB(浮动操作按钮)和一个用于 FAB 按钮阴影的视图,如下所示:
    <?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.floatingactionbutton.FloatingActionButton
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_gravity="center"
           android:id="@+id/fab"
           android:src="@drawable/ic_add"/>
        <View
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:background="@drawable/shadow_bg"
            app:layout_anchor="@id/fab"
            app:layout_anchorGravity="center"
            android:padding="10dp"/>
    
        </androidx.coordinatorlayout.widget.CoordinatorLayout>

这将显示为:

enter image description here

享受吧,你已经可以开始了。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接