启动应用程序后,为什么我的浮动操作按钮会跳到页面顶部?

时间:2019-04-07 13:31:57

标签: android xml

我目前在Android应用程序中有一个回收站视图和一个浮动操作按钮的问题。启动应用程序时,该按钮始终跳到页面的右上角。

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

布局预览:

Layout preview

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

Screen on my phone

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

<?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>

提前感谢您的帮助!

3 个答案:

答案 0 :(得分:0)

尝试在CoordinatorLayout中使用recyclerview和fab按钮。

属性app:layout_anchorGravity:将指定对象应如何在其父级边界内的X和Y轴上相对于锚定位。

<android.support.design.widget.CoordinatorLayout
  android:id="@+id/fragment_bucketlist"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  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/floating_action_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="16dp"
    android:scaleType="center"
    android:src="@drawable/android_floating_action_button_icon"
    app:layout_anchor="@id/rv_bucketList"
    app:layout_anchorGravity="bottom|right|end"
    app:borderWidth="1dp"
    app:elevation="10dp" />

   </android.support.design.widget.CoordinatorLayout>

答案 1 :(得分:0)

我使用RelativeLayout解决了这个问题。有一个问题,即“浮动操作按钮”始终显示在底部边缘,并“卡在”底部导航栏(HUAWEI设备)上。属性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>

答案 2 :(得分:-1)

试试这个。将constrainLayout更改为relativeLayout

<RelativeLayout android:id="@+id/fragment_bucketlist"
    android:layout_width="match_parent"
    android:background="@color/colorPrimary"
    android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

<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"
    android:layout_alignParentBottom="true"
    android:layout_marginBottom="50dp"
    android:layout_marginEnd="50dp"
    android:layout_alignParentEnd="true"
    app:fabSize="normal"/>

</RelativeLayout>