[英]Can't make Floating Action Button “float”
首先,我檢查了以下答案: 如何在android支持設計庫隨附的FAB中添加陰影?
但是,即使添加app:borderWidth="0dp"
或elevation="6dp"
也不起作用。 我已經檢查了這個答案: https : //stackoverflow.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中,然后將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,陰影就會消失。 因此,陰影只在一個范圍內起作用。
也許您可以將其理解為一個對象,將陰影投射到底層對象上。 高度越高,兩個對象之間的距離越大,投射的陰影越少...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.