8

我想使用 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 然后在屏幕顶部溢出(但底部没有被覆盖不再)。

下面是 BottomAppBar 覆盖按钮的 xml 布局文件。谢谢

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

BottomAppBar 覆盖按钮

4

1 回答 1

1

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

android:layout_marginBottom="?attr/actionBarSize"

例如

<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>
于 2020-11-25T08:21:14.060 回答