android - float 操作按钮在 Lollipop 之前的设备上位置错误

标签 android material-design android-support-library

我有一个奇怪的行为问题:

在任何版本低于 Lollipop 的设备或模拟器上, float 操作按钮和 float 在工具栏上的其他组件(例如微调器的弹出窗口)的位置都是错误的。它们似乎根据 app:elevation 的值移动,但即使 app:elevation="0"也不会将它们定位为与 Lollipop 设备相同。

编辑: 我只是想更好地解释 FAB 的行为。当我将预 Lollipop 设备上的海拔标签添加到 fab 时,其 x 和 y 坐标会增加。在 Lollipop 设备上,海拔标签对位置没有影响。

编辑2: 经过一些实验,我发现问题在于,在 Lollipop 设备上绘制阴影时布局尺寸会增加。这也增加了可点击区域。这种行为使得无法为 Lollipop 和预 Lollipop 设备创建 2 种不同的布局,因为 FAB 的行为和大小是错误的。在 Lollipop 设备上,布局为(根据需要)40dp * 40dp,FAB 在其下方的布局上绘制阴影。

布局:

<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:id="@+id/id_drawer_layout">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.Toolbar
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        android:layout_height="128dp"
        android:layout_width="match_parent"
        android:background="@color/primaryColor"
        android:id="@+id/id_toolbar"
        android:elevation="20dp"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light">

        <Spinner
            android:layout_marginStart="12dp"
            android:layout_marginTop="62dp"
            android:id="@+id/id_toolbar_spinner"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

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

    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/id_swipe_refresh_layout"
        android:layout_below="@id/id_toolbar"
        android:layout_height="match_parent"
        android:layout_width="match_parent">

        <android.support.v7.widget.RecyclerView
            android:layout_below="@id/id_toolbar"
            android:layout_height="match_parent"
            android:layout_width="match_parent"
            android:id="@+id/id_filemanager_recycler_view"
            android:background="@android:color/white"/>

    </android.support.v4.widget.SwipeRefreshLayout>

    <android.support.design.widget.FloatingActionButton
        android:layout_below="@id/id_toolbar"
        android:layout_marginTop="-20dp"
        android:layout_marginStart="16dp"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:id="@+id/id_floating_action_button"
        android:src="@drawable/ic_add_white_24dp"
        app:elevation="30dp"
        app:borderWidth="0dp"
        app:fabSize="mini"/>
</RelativeLayout>

<android.support.design.widget.NavigationView
    android:layout_width="288dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:background="@android:color/white"
    android:id="@+id/id_main_navigation_view"
    app:menu="@menu/menu_drawer_main"/>

预 Lollipop enter image description here

Lollipop enter image description here

最佳答案

我也遇到了同样的问题。解决方案是将 FloatingActionButton 放入 android.support.design.widget.CoordinatorLayout 中。

这就是我正在使用的

<android.support.design.widget.CoordinatorLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right|bottom"
        android:layout_margin="16dp"
        android:src="@drawable/ic_add_white_24dp"
        app:elevation="6dp"
        app:fabSize="normal"
        app:pressedTranslationZ="12dp" />
</android.support.design.widget.CoordinatorLayout>

您可以修改此代码以满足您的需求。

关于android - float 操作按钮在 Lollipop 之前的设备上位置错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32615630/

相关文章:

android - 4个Android按钮是标准的吗?前任。后退键、菜单键

java - Android L - float 操作按钮 (FAB)

android - 在文本输入布局android之外单击时如何将 float 标签文本重置为其原始位置

带有 fragment 的 Android InstantiationException(它是公开的)

android - 处于展开状态时无法使支持底板不可隐藏

java - Android 中的应用程序可以在不打开电子邮件界面的情况下发送电子邮件吗?

java - Android 在线流媒体应用程序不应在屏幕锁定和解锁时关闭

Android webview 性能,我错过了什么?

css - Material Components Web - 字体大小 body1 body2 在桌面上太小

android - 配置更改后 Activity 引用错误的嵌套 fragment