如何防止BottomAppBar重叠内容

Pao*_*man 8 xml android android-layout android-bottomappbar

我想使用 CoordinatorLayout 在屏幕底部包含一个 BottomAppBar,并在剩余空间中显示一些内容(例如使用 ConstraintLayout)。

如果我向 CoordinatorLayout 添加一个 ConstraintLayout,然后我添加一个放置在 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>
Run Code Online (Sandbox Code Playgroud)

BottomAppBar 覆盖按钮

Muz*_*Muz 2

我还没有找到合适的解决方案,但对我有用的是在与 BottomAppBar 重叠的边距上添加边距,

android:layout_marginBottom="?attr/actionBarSize"
Run Code Online (Sandbox Code Playgroud)

例如

<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>
Run Code Online (Sandbox Code Playgroud)