如何防止BottomAppBar覆盖内容

10
我想使用CoordinatorLayout将BottomAppBar放置在屏幕底部,并在剩余空间中显示一些内容(例如使用ConstraintLayout)。如果我将一个ConstraintLayout添加到CoordinatorLayout中,然后添加一个位于ConstraintLayout底部的按钮,该按钮会被BottomAppBar覆盖。我希望ConstraintLayout占用除了BottomAppBar所在位置以外的所有垂直空间,这样按钮就不会被覆盖。我尝试在ConstraintLayout中包含app:layout_behavior="@string/appbar_scrolling_view_behavior",如一些网站所建议的,但它并不起作用。同时,在BottomAppBar中设置app:layout_insetEdge="bottom",然后在ConstraintLayout中设置app:layout_dodgeInsetEdges="bottom"并不能正常工作,因为这样会导致ConstraintLayout溢出到屏幕顶部(但底部不再被覆盖)。下面是xml布局文件,其中BottomAppBar覆盖了按钮。谢谢
    <?xml version="1.0" encoding="utf-8"?>

    <androidx.coordinatorlayout.widget.CoordinatorLayout 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:id="@+id/coordinatorLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="top"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <Button
        android:id="@+id/myButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

    </androidx.constraintlayout.widget.ConstraintLayout>

    <com.google.android.material.bottomappbar.BottomAppBar
        android:id="@+id/bottomAppBar"
        style="@style/Widget.MaterialComponents.BottomAppBar.Colored"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom" />    
    </androidx.coordinatorlayout.widget.CoordinatorLayout>

The BottomAppBar is covering the button

1个回答

2

我还没有找到一个合适的解决方法,但是对我来说有效的方法是给与 BottomAppBar 重叠的那个元素添加一个 margin。

android:layout_marginBottom="?attr/actionBarSize"

e.g.

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"     
    android:layout_height="match_parent"
    android:layout_gravity="top"
    android:layout_marginBottom="?attr/actionBarSize"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <Button
        android:id="@+id/myButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接