首页 > 解决方案 > 为什么我的浮动操作按钮在应用启动时会跳到页面顶部?

问题描述

目前,我的 android 应用程序中的回收站视图和浮动操作按钮存在问题。当我启动应用程序时,按钮总是跳到页面的右上方。

RecyclerView 和 Button 位于片段内。目前我正在使用 ConstraintLayout 来表示 Button 应该始终出现在屏幕的右端。但是,我也尝试使用具有锚点和重力属性的 RelativeLayout 和 CoordinatorLayout。在 Android Studio 中,布局显示正确,按钮位于屏幕右下方。当我在手机上启动应用程序时,它就会出现在屏幕的右上角...

布局预览:

布局预览

当我在手机上启动应用程序时:

我的手机屏幕

这是我现在用于片段的代码:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/fragment_bucketlist"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    android:background="@color/colorPrimary">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/rv_bucketList"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab_addToBucketList"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintRight_toRightOf="parent"/>

</android.support.constraint.ConstraintLayout>

提前感谢您的帮助!

标签: androidxml

解决方案


我使用RelativeLayout 解决了这个问题。存在浮动操作按钮始终显示在底部边缘并“卡在”底部导航栏(华为设备)的问题。属性 app:useCompatPadding="true" 解决了这个问题。

这是更新的代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:app="http://schemas.android.com/apk/res-auto"
   android:id="@+id/fragment_bucketlist"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   app:layout_behavior="@string/appbar_scrolling_view_behavior"
   android:background="@color/colorPrimary">

<android.support.v7.widget.RecyclerView
    android:id="@+id/rv_bucketList"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab_addToBucketList"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_edit"
    app:useCompatPadding="true"
    android:layout_alignParentBottom="true"
    android:layout_alignParentEnd="true" />

 </RelativeLayout>

推荐阅读