【问题标题】:How to anchor FAB to BottomSheet with margins?如何使用边距将 FAB 锚定到 BottomSheet?
【发布时间】:2021-08-09 14:22:28
【问题描述】:

我的 APP 有问题,我有一个 BottomSheet,我正在添加一个 FAB,FAB 应该始终位于 BottomSheet 的顶部,所以我在 FAB 中添加了底部边距,问题是,一旦 BottomSheet 展开,FAB 就会锚定到 bottomsheet 而没有初始底部边距。

我怎样才能使FAB 始终位于顶部?

这是我的代码:

<androidx.coordinatorlayout.widget.CoordinatorLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="2dp"
        android:padding="2dp"
        android:scrollbars="vertical"
        tools:listitem="@layout/recyclerview_pterm" />

    <include
        layout="@layout/bottom_sheet_pterm" />

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/floatingActionButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        android:layout_marginBottom="76dp"
        android:clickable="true"
        android:focusable="true"
        app:layout_anchor="@+id/bottomSheet"
        app:layout_anchorGravity="top|end"
        app:srcCompat="@drawable/ic_covid"
        android:contentDescription="@string/verifica_gp" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

下面是它的样子:

【问题讨论】:

    标签: android android-layout floating-action-button bottom-sheet


    【解决方案1】:

    您可以通过将这两个属性添加到 FAB 来解决这个问题:

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        ....
        app:useCompatPadding="true"
        android:layout_gravity="top" />
    

    android:layout_gravity="top" 使 FAB 位于锚点的顶部(即不与其相交),app:useCompatPadding="true" 允许 FAB 和锚点之间的填充(即 bottomSheet)。

    示例:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-08-14
      • 2022-10-18
      • 1970-01-01
      • 1970-01-01
      • 2015-05-29
      • 1970-01-01
      • 2015-11-25
      • 1970-01-01
      相关资源
      最近更新 更多