【问题标题】:Can't make Floating Action Button "float"无法使浮动操作按钮“浮动”
【发布时间】:2016-11-26 07:46:10
【问题描述】:

首先,我已经检查了答案: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 它说我的高度越大,阴影越大,有趣的是,在预览屏幕上它显示了阴影,但是在智能手机上运行时我没有阴影。

以下是智能手机的屏幕截图:

下面是 android studio 的预览屏幕截图:

我的布局代码:

<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"/>

【问题讨论】:

    标签: android android-layout layout material-design floating-action-button


    【解决方案1】:

    好的,所以我尝试了一些,但高程阴影似乎不像你想象的那样工作。这段代码给了相当大的阴影:

    <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 是你能得到的最大阴影。
    【解决方案2】:

    尝试将您的布局包装在 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'
    

    【讨论】:

      猜你喜欢
      • 2015-06-25
      • 2020-05-28
      • 1970-01-01
      • 2018-10-18
      • 2015-09-01
      • 1970-01-01
      • 2015-11-08
      • 1970-01-01
      • 2015-11-11
      相关资源
      最近更新 更多