如何去除浮动操作按钮周围的阴影?

How to remove FloatingActionButton's surrounding shadow?

我正在尝试替换库 com.android.support:design:22.2.0 中打包的第三方 FloatingActionButton with the native one。默认外观在图像周围有一个暗影,我怎样才能摆脱它?我知道前者提供了方法setShadow(),但我就是找不到后者的类似方法。

这是相关的 XML 布局:

<android.support.design.widget.FloatingActionButton
        android:id="@+id/alarm_front"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/btn_icon_alarm_notset" />

并且我已经将它的背景颜色设置为黄色。

mAlarmBtn.setBackgroundTintList(ColorStateList.valueOf(getResources().getColor(R.color.floatButtonColor)));

通过添加以下内容覆盖 FAB 的默认 elevation

android:elevation="0dp"

或者在代码调用中View.setElevation(float)

通过添加以下内容覆盖 FAB 的默认高度:

app:elevation="0dp"

如果您正在使用支持库 - 最新的 Android Studio 模板为我们提供。 检查进口

import android.support.design.widget.FloatingActionButton;
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
//if using support app compat
fab.setCompatElevation(16.0f);

否则如果你只支持较新的 sdk 版本

fab.setElevation();
//call requires SDK 21

.../app/build.gradle
  minSdkVersion 18    << less than 21 so req support libraries
  targetSdkVersion 25

添加这个

android:elevation="0dp" app:elevation="0dp"

它会像:

 <android.support.design.widget.FloatingActionButton
        android:id="@+id/floatingActionButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_add"
        android:elevation="0dp"
        app:elevation="0dp"
        app:fabSize="normal"
        android:scaleType="fitCenter"/>

尝试了以上所有建议,但对 API 23 及更高版本没有任何效果。我已经结束了这个,它已经完全消除了阴影:

app:backgroundTint="@android:color/transparent"
app:borderWidth="0dp"

下面是我的按钮现在的样子:

更改前如下所示:

将 borderWidth 设置为 0

app:borderWidth="0dp"

此解决方案也适用于 ExtendedFloatingActionButton:

style="@style/Widget.MaterialComponents.Button.UnelevatedButton"