0

首先第一件事情,我查過這個問題的答案:How to add shadow to the FAB provided with the android support design library?不能使浮動操作按鈕「浮動」

但即使添加了app:borderWidth="0dp"elevation="6dp"沒有奏效。我檢查了這個答案:https://stackoverflow.com/a/30752754/1121139它說我的高度越大,影子越大,這裏有趣的是,在預覽屏幕上它顯示了影子,但是當在智能手機上運行時,我沒有影子。

這裏去從智能手機的屏幕截圖: enter image description here

,並在這裏機器人工作室去和截圖,從預覽畫面: enter image description here

我的佈局代碼:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" 
tools:context="amaz1ngc0de.com.br.materialdesign.MainActivity"> 

<include android:id="@+id/app_bar" layout="@layout/toolbar_app_bar"/> 

<android.support.v7.widget.RecyclerView 
    android:id="@+id/rv_test_fab" 
    android:layout_below="@id/app_bar" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 

</android.support.v7.widget.RecyclerView> 

<android.support.design.widget.FloatingActionButton 
    android:layout_margin="16dp" 
    android:layout_alignParentRight="true" 
    android:layout_alignParentBottom="true" 
    android:src="@drawable/ic_add_white_24dp" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 

    app:elevation="140dp" 
    app:borderWidth="0dp" 
    app:pressedTranslationZ="12dp" 
    android:clickable="true"/> 

回答

0

嘗試將您的佈局包裹在CoordinatorLayout中d將FAB在同一水平,而不是一個RelativeLayout的,例如:

<!-- main_layout.xml --> 
<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.CoordinatorLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:animateLayoutChanges="true" 
    android:fitsSystemWindows="true" 
    tools:context=".activity.MainActivity"> 

    <include layout="@layout/toolbar_app_bar" /> 

    <android.support.v7.widget.RecyclerView 
     android:id="@+id/rv_test_fab" 
     android:layout_below="@id/app_bar" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"/> 

    <android.support.design.widget.FloatingActionButton 
     ... /> 

</android.support.design.widget.CoordinatorLayout> 

編輯: 這個小工具從設計庫,你應該在你的應用程序的build.gradle文件補充說:

compile 'com.android.support:design:24.0.0' 
0

好吧,我已經嘗試了一下,它似乎與仰角陰影不符合你的想象。此代碼給出了相當的影子:

<android.support.design.widget.FloatingActionButton 
    android:id="@+id/name_add" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="bottom|end" 
    android:layout_margin="15dp" 
    android:src="@drawable/ic_add" 
    app:elevation="20dp"/> 

但是,如果將標高設置爲200,則陰影消失。所以影子的工作範圍只有一個範圍。

也許你可以將它理解爲一個對象,將陰影投射到下面的物體上。海拔越高,兩個物體之間的距離越大,投射的陰影就越少...

+0

我嘗試了一下dp,看起來在60dp時陰影開始再次消失。所以40dp或50dp是你能得到的最大影子。 –