如何去除FloatingActionButton周围的阴影?

39
我正尝试将第三方FloatingActionButton替换为原生的按钮,后者被打包在库com.android.support:design:22.2.0中。默认外观会在图像周围产生深色阴影,如何去掉它?我知道前者提供了setShadow()方法,但我无法从后者中找到类似的方法。

enter image description here

这是相关的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)));
7个回答

55

3
setElevation的最低API级别为21,那么在LOLLIPOP以下的版本中如何使用它? - tounaobun
在Lollipop之前,您可以简单地坚持使用旧方法创建自己的ImageButton。就像在引入FAB之前一样。 - Zsolt Boldizsar
其他开发人员报告了有关新支持 FAB 的阴影问题,尽管通常情况下阴影没有显示,这是相反的问题,但可能是相关问题。https://dev59.com/FV0a5IYBdhLWcg3wRGzp - BrentM
22
使用官方提供的 FAB,您可以使用 app:elevation="0dp" 来实现您想要的效果。我在 Android 4.1.1 上尝试了一下,效果很好。我不知道是否已经有 Java 解决方案。 - Gaëtan
33
为了兼容性,请使用 app:elevation="0dp" - Leonardo
显示剩余2条评论

38

添加这个:

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"/>

这是所有答案中的答案。 - GvSharma

26

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

app:elevation="0dp"

8
将borderWidth设置为0。
app:borderWidth="0dp"

尝试了以上所有方法,但阴影仍然存在...这个答案对我有用! - stefan

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版本,则需要使用“else if”语句。

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

查看

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

4
尝试了上述所有建议,API 23及更高版本仍无法解决。我最终使用了以下方法,完全去除了阴影:
app:backgroundTint="@android:color/transparent"
app:borderWidth="0dp"

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

enter image description here

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

enter image description here


2

这个解决方案也适用于ExtendedFloatingActionButton:

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

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