【问题标题】:Issue with CoordinatorLayout and FloatingActionButtonCoordinatorLayout 和 FloatingActionButton 的问题
【发布时间】:2020-12-07 09:37:27
【问题描述】:

picture 1 上看起来不错,但在picture 2 上,因为FAB 有app:layout_anchor="@id/recyclerview_list" 属性,所以看起来不太好。有没有办法在不添加自定义底部导航栏的情况下将 FAB 锚定到底部?即使 RecyclerView 改变它的高度,我只想让这个按钮永久保持在位置上。

这是 XML 文件:

<androidx.coordinatorlayout.widget.CoordinatorLayout
android:id="@+id/main_layout"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="fragments.DailyListFragment">

<com.google.android.material.appbar.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/appbar_layout"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <com.google.android.material.appbar.CollapsingToolbarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_scrollFlags="scroll|snap|exitUntilCollapsed"
        app:title="@string/sample"
        app:contentScrim="@color/accent_color1">

        <ProgressBar
            android:id="@+id/progressBar2"
            style="?android:attr/progressBarStyleHorizontal"
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:progressTint="#BA0E0E" />

        <androidx.appcompat.widget.Toolbar
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Dark"/>

    </com.google.android.material.appbar.CollapsingToolbarLayout>

</com.google.android.material.appbar.AppBarLayout>

<com.google.android.material.floatingactionbutton.FloatingActionButton
    android:id="@+id/fab_new_item"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="24dp"
    android:layout_marginBottom="24dp"
    android:src="@drawable/ic_add_black_24dp"
    android:visibility="visible"
    app:fabSize="normal"
    app:layout_anchor="@id/recyclerview_list"
    app:layout_anchorGravity="bottom|right|end"/>

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/recyclerview_list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:alwaysDrawnWithCache="false"
    android:background="@color/colorBackground"
    android:visibility="visible"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:listitem="@layout/recyclerview_layout_list">

</androidx.recyclerview.widget.RecyclerView>

</androidx.coordinatorlayout.widget.CoordinatorLayout>

【问题讨论】:

  • 如果你想让它一直在底部,你为什么要把它锚定到recyclerview?卸下锚应该可以工作。如果任何用例需要,请添加更多详细信息。
  • android:layout_gravity="bottom|end"替换app:layout_anchor="@id/recyclerview_list" app:layout_anchorGravity="bottom|right|end"

标签: android xml android-recyclerview floating-action-button android-coordinatorlayout


【解决方案1】:

替换

<com.google.android.material.floatingactionbutton.FloatingActionButton
    app:layout_anchor="@id/recyclerview_list" 
    app:layout_anchorGravity="bottom|right|end"

<com.google.android.material.floatingactionbutton.FloatingActionButton
    android:layout_gravity="bottom|end"

【讨论】:

    猜你喜欢
    • 2015-08-21
    • 1970-01-01
    • 2015-12-17
    • 2015-10-11
    • 1970-01-01
    • 2015-08-28
    • 1970-01-01
    • 2015-11-13
    • 1970-01-01
    相关资源
    最近更新 更多