【问题标题】:Floating Action Button in wrong position浮动操作按钮位置错误
【发布时间】:2016-09-08 10:49:11
【问题描述】:

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

This is how it should look

This is how it somtetimes look when i open the 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>

有人知道为什么 FAB 有时会移动到错误的位置吗?

【问题讨论】:

  • 根据您的 xml,有回收站视图,但屏幕截图显示 textview?
  • @Sangram textview 是一个 ViewHolder。当回收站视图为空时显示。
  • 您也可以通过编程方式将重力设置为 FAB。检查我的答案。
  • @Cimoe 你解决了这个问题我面临同样的问题
  • 把FAB放到第一个孩子是解决问题(v25)

标签: java android floating-action-button


【解决方案1】:

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

我可以通过从工厂中删除 app:layout_anchor 属性或恢复到旧版本的设计库来解决此问题。 24.1.1 对我来说效果很好。我还必须将我的 appcompat v7 库恢复为 24.1.1

我希望这会有所帮助。

【讨论】:

  • 我可以确认同样的问题,我可以通过删除 app:layout_anchor 来解决
【解决方案2】:

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

【讨论】:

  • 我有一个非常相似的布局,并且无法将 FAB 移动为第一个孩子,layout_anchor 属性如何像现在在 LinearLayout 之前一样工作?
  • layout_anchor 无论如何都应该可以工作,无论它是在锚定视图的上方还是下方。唯一的限制是具有 layout_anchor 属性的视图的父级应该是 CoordinatorLayout
  • 我刚刚注意到 API19 按钮出现在下一个子项的后面
  • 这很奇怪,但它甚至适用于 24.2.0+。只有一个缺点 - 锚点和重力应该以编程方式设置。
  • 好的,我发现了真正的缺点 - 在最旧的设备中,如果您在 fab 之后有 match_parent 可点击视图,此视图将处理点击,因此无法点击 fab
【解决方案3】:

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

【讨论】:

  • 感谢您的想法,但仍然无法正常工作。晶圆厂仍在随机移动到错误的位置
【解决方案4】:

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

【讨论】:

    【解决方案5】:

    试试这个:

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

    【讨论】:

      【解决方案6】:

      这些建议都没有最终对我有用(降级不是一种选择;将 FAB 向上移动使其成为第一个孩子使其在 Android 14 上不可见;删除 layout_anchor 使其始终跳转到随机位置)

      我改为使用 ConstraintLayout 在我想要的两个视图之间手动定位 FAB。由于我的整个屏幕没有滚动区域,所以效果很好。

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

      【讨论】:

        【解决方案7】:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-10-21
          • 2016-07-26
          • 2016-10-10
          • 1970-01-01
          • 1970-01-01
          • 2020-12-29
          • 2015-12-13
          • 1970-01-01
          相关资源
          最近更新 更多