繁体   English   中英

浮动操作按钮位置错误

[英]Floating Action Button in wrong position

我在我的两个Fragment布局中有浮动动作按钮,有时它们移动到错误的位置,尽管我使用CoordinatorLayout。

这应该是它的样子

当我打开碎片时,这就是它看起来的样子

这是我片段的代码:

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

有谁知道为什么FAB有时会走向错误的位置?

我遇到了同样的问题。 问题在于app:layout_anchorcom.android.support.design:24.2.0+的组合

我能够通过从fab中删除app:layout_anchor属性或者恢复到旧版本的设计库来解决此问题。 24.1.1对我来说很好。 我还必须将appcompat v7库还原为24.1.1

我希望这有帮助。

尝试让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>

CoordinatorLayout.LayoutParams类具有anchorGravity字段。 例如:

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

在我的情况下,这是24.2.0及更高版本的支持库中的错误。 因此,解决它的唯一方法是使用支持lib v24.1.1构建应用程序。

尝试这个:

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

这些建议都没有结束为我工作(降级不是一个选项;移动FAB,因此它是第一个让它在Android 14上看不见的孩子;删除layout_anchor使它一直跳到随机位置)

我改为使用ConstraintLayout在我想要的两个视图之间手动定位FAB。 由于我的整个屏幕没有滚动区域,因此工作正常。

要以这种方式定位FAB,请将其顶部和底部约束设置为它应该重叠的上部视图之一的底部,并确保它是ConstraintLayout中的最后一个子视图。

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

暂无
暂无

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

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