Đối với API 21+ bạn cần đặt app:borderWidth="0dp"
và app:elevation="[number]dp"
. Đặt độ cao bạn đang đưa ra kích thước của bóng mà bạn muốn:
Dưới đây là một ví dụ về mã cho API trên 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">
Một điều quan trọng cần nhớ cho API dưới đây để 21 (Android 4), là dành cho các điều khoản tương thích FAB sẽ đặt một lề xung quanh nút của bạn để vẽ bóng. Sau đó, bạn nên làm một cái gì đó như thế (Tôi hiện đang sử dụng đoạn mã này và công trình):
<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">
Tôi thích đặt xmlns:app="http://schemas.android.com/apk/res-auto"
vào đầu của XML, nhưng tôi đặt ở đó chỉ để nhắc nhở bạn;]
Nguồn
2015-06-10 09:44:24
Ít nhất là 21+, điều này có thể áp dụng: https://stackoverflow.com/questions/27080338/android-5-0-androidelevation-works-for-view-but-not-button – CommonsWare
Điều tôi đã cân nhắc nhưng loại bỏ các StateListAnimator không mang lại bóng tối. Điều này có thể là do FAB thực sự là một ImageView mở rộng không phải là một Button. Lạ vì FAB thực sự có một StateListAnimator. –