不能使浮动操作按钮“浮动”
问题描述:
首先第一件事情,我查过这个问题的答案:How to add shadow to the FAB provided with the android support design library?不能使浮动操作按钮“浮动”
但即使添加了app:borderWidth="0dp"
或elevation="6dp"
没有奏效。我检查了这个答案:https://*.com/a/30752754/1121139它说我的高度越大,影子越大,这里有趣的是,在预览屏幕上它显示了影子,但是当在智能手机上运行时,我没有影子。
我的布局代码:
<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"/>
答
尝试将您的布局包裹在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'
答
好吧,我已经尝试了一下,它似乎与仰角阴影不符合你的想象。此代码给出了相当的影子:
<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,则阴影消失。所以影子的工作范围只有一个范围。
也许你可以将它理解为一个对象,将阴影投射到下面的物体上。海拔越高,两个物体之间的距离越大,投射的阴影就越少...
我尝试了一下dp,看起来在60dp时阴影开始再次消失。所以40dp或50dp是你能得到的最大影子。 –