33

我正在尝试将第三方FloatingActionButton替换为库中打包的本机com.android.support:design:22.2.0按钮。默认外观在图像周围有一个深色阴影,我该如何摆脱它?我知道前者提供了该方法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)));
4

7 回答 7

52

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

android:elevation="0dp"

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

于 2015-06-03T07:57:00.007 回答
34

添加这个

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"/>
于 2017-11-30T07:22:05.817 回答
26

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

app:elevation="0dp"
于 2017-04-10T14:41:29.960 回答
8

将边框宽度设为 0

app:borderWidth="0dp"
于 2019-04-27T06:24:27.843 回答
5

如果您正在使用支持库 - 最新的 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
于 2017-05-12T14:33:10.840 回答
4

尝试了上述所有建议,但对于 API 23 及更高版本均无效。我最终得到了这个,它完全消除了阴影:

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

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

在此处输入图像描述

在更改之前,它看起来如下:

在此处输入图像描述

于 2018-02-02T04:51:14.223 回答
0

此解决方案也适用于 ExtendedFloatingActionButton:

style="@style/Widget.MaterialComponents.Button.UnelevatedButton"
于 2021-03-02T13:36:43.733 回答