简体   繁体   English

浮动操作按钮位置错误

[英]Floating Action Button in wrong position

I have Floating Action Buttons in two of my Fragment layouts and sometimes they move to the wrong position, altough I use CoordinatorLayout. 我在我的两个Fragment布局中有浮动动作按钮,有时它们移动到错误的位置,尽管我使用CoordinatorLayout。

This is how it should look 这应该是它的样子

This is how it somtetimes look when i open the Fragment 当我打开碎片时,这就是它看起来的样子

This is the code of my fragment: 这是我片段的代码:

<?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"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout android:id="@+id/notes_layout"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".fragments.NotesFragment">

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

</LinearLayout>

<android.support.design.widget.FloatingActionButton
    android:id="@+id/new_note_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="16dp"
    android:clickable="true"
    android:src="@drawable/ic_add_black_48dp"
    app:layout_anchor="@id/notes_layout"
    app:layout_anchorGravity="bottom|center"
    android:onClick="openNewNote"/>

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

Does anyone have an idea why the FAB sometimes move to the wrong position? 有谁知道为什么FAB有时会走向错误的位置?

I ran into the same issue. 我遇到了同样的问题。 The problem is with the combination of app:layout_anchor and com.android.support.design:24.2.0+ 问题在于app:layout_anchorcom.android.support.design:24.2.0+的组合

I was able to fix this issue by either removing the app:layout_anchor property from the fab or by reverting to an older version of the design library. 我能够通过从fab中删除app:layout_anchor属性或者恢复到旧版本的设计库来解决此问题。 24.1.1 worked fine for me. 24.1.1对我来说很好。 I also had to revert my appcompat v7 library to 24.1.1 . 我还必须将appcompat v7库还原为24.1.1

I hope this helps. 我希望这有帮助。

Try to make FAB first child of CoordinatorLayout 尝试让FAB成为CoordinatorLayout第一个孩子

<android.support.design.widget.CoordinatorLayout
            android:id="@+id/main_content"
            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.support.design.widget.FloatingActionButton
            .../>

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

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

The CoordinatorLayout.LayoutParams class has the anchorGravity field for that. CoordinatorLayout.LayoutParams类具有anchorGravity字段。 For example: 例如:

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
CoordinatorLayout.LayoutParams lp = (CoordinatorLayout.LayoutParams) fab.getLayoutParams();
lp.anchorGravity = Gravity.BOTTOM | GravityCompat.START;
fab.setLayoutParams(lp);

In my case this was a bug in support library from 24.2.0 and above. 在我的情况下,这是24.2.0及更高版本的支持库中的错误。 So, the only way to workaround it was to build the app with support lib v24.1.1. 因此,解决它的唯一方法是使用支持lib v24.1.1构建应用程序。

try this: 尝试这个:

    <android.support.design.widget.CoordinatorLayout
            android:id="@+id/main_content"
            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.support.v7.widget.RecyclerView
             android:id="@+id/rvToDoList"
             android:layout_width="match_parent"
             android:layout_height="match_parent">

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

       <android.support.design.widget.FloatingActionButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|right"
            android:layout_margin="16dp"
            android:src="@mipmap/ic_launcher"
            app:layout_anchor="@id/rvToDoList"
            app:layout_anchorGravity="bottom|right|end"/>

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

None of these suggestions ended up working for me (downgrading was not an option; moving the FAB up so it's the first child makes it invisible on Android 14; removing layout_anchor makes it jump to a random position all the time) 这些建议都没有结束为我工作(降级不是一个选项;移动FAB,因此它是第一个让它在Android 14上看不见的孩子;删除layout_anchor使它一直跳到随机位置)

I instead used ConstraintLayout to position the FAB manually between the two views I wanted it at. 我改为使用ConstraintLayout在我想要的两个视图之间手动定位FAB。 Since the entire screen of mine has no scrolling area, that worked fine. 由于我的整个屏幕没有滚动区域,因此工作正常。

To position the FAB this way, set both its top and bottom constraint to the bottom of one of the upper view it's supposed to overlap and make sure it is the last child in the ConstraintLayout. 要以这种方式定位FAB,请将其顶部和底部约束设置为它应该重叠的上部视图之一的底部,并确保它是ConstraintLayout中的最后一个子视图。

将FAB放在RelativeLayout中,它对我有用,我使用的是支持库的25.3.1版本

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM