android支持设计库提供的FAB如何添加阴影?

How to add shadow to the FAB provided with the android support design library?

这个标题很容易解释。

以下代码不会在浮动操作按钮下方呈现阴影。渲染阴影可以做什么? API 21+ 是否真的不支持此功能?

<android.support.design.widget.FloatingActionButton
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:src="@drawable/ic_add"
    android:clickable="true" />

注意:添加 android:elevation 不会在 API 上添加阴影 21.

dandar3 从示例中截取的屏幕截图: https://github.com/dandar3/android-support-design

只需设置 app:borderWidth="0dp" 即可为我解决此问题。

注意:不要忘记将 xmlns:app="http://schemas.android.com/apk/res-auto" 添加到您的根布局。

这个 issue 应该在下一版本的 android 设计库中修复。

对于API 21+你需要设置app:borderWidth="0dp"app:elevation="[number]dp"。设置海拔你给你想要的阴影大小:

下面是 API 21+ 的代码示例:

<android.support.design.widget.FloatingActionButton
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/locate_user_FAB"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/location_off"
    app:elevation="6dp"
    app:borderWidth="0dp"
    android:layout_above="@+id/take_taxi_FAB"
    app:backgroundTint="@color/colorAccentGrey">

对于下面的 APIs 到 21 (Android 4),要记住一件重要的事情是为了兼容性 FAB 将在您的按钮周围放置一个边距以绘制阴影。那么你应该做类似的事情(我目前正在使用这个代码并且有效):

<android.support.design.widget.FloatingActionButton
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/locate_user_FAB"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/location_off"
    app:elevation="6dp"
    app:borderWidth="0dp"
    android:layout_above="@+id/take_taxi_FAB"
    android:layout_alignParentRight="true"
    android:layout_marginRight="@dimen/map_FAB_marginRight"
    android:layout_marginBottom="@dimen/locate_user_FAB_marginBottom"
    app:backgroundTint="@color/colorAccentGrey">

我更喜欢把 xmlns:app="http://schemas.android.com/apk/res-auto" 放在 XML 的开头,但我放在那里只是为了提醒你 ;]

如果这对某些人仍然不起作用,则两者之间存在显着差异:

app:elevation="6dp"
app:borderWidth="0dp"

app:borderWidth="0dp"
app:elevation="6dp"

出于某种原因,顺序似乎很重要(第一个顺序有效,第二个无效),这是来自支持库 23.3.0

检查应用程序标签中项目或库中的清单并删除它们

android:hardwareAccelerated="false"
android:largeHeap="true"

但是如果您需要这些选项,那么阴影和变换动画将不起作用

我遇到了同样的问题,我通过从我的 AndroidManifest.xml 中删除这个标签来解决这个问题。

android:hardwareAccelerated="false"

我最初将它与 android:largeHeap="true" 一起添加,因为我认为我需要它用于显示大量点的热图,但后来我意识到它可以仅与 [=11= 一起使用].

如果有帮助,我正在使用

android:tint="@color/myColor"

而不是

android:backgroundTint="@color/myColor".

用 backgroundTint 替换色调恢复了阴影。