Bottomappbar 未正确显示

Bottomappbar not seen properly

我正在尝试将 bottomAppBar 放入 mainActivity。 所以我在这里尝试并检查了很多东西,但找不到任何解决方案。

MainActivity 的 XML 代码如下(它有另一个约束布局作为父级但没有在这里添加。)

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

    <com.google.android.material.bottomappbar.BottomAppBar
        android:id="@+id/bottomAppbar"
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:layout_gravity="bottom"
        android:backgroundTint="@color/colorGray"
        app:fabAlignmentMode="center" />

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_anchor="@id/bottomAppbar"
        app:fabCradleRoundedCornerRadius="-10dp"
       />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

所以在那之后,我在 menu 目录中准备了一个 bottomappbar_menu.xml 并尝试将此菜单附加到我的 MainActivity 文件的 OnCreate 方法中,如下所示:

bottomAppbar.replaceMenu(R.menu.bottomappbar_menu)

另外styles.xmsl如下,

<resources>
    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

</resources>

但是当我安装到phone时,它如下所示。我没有看到任何菜单项:

看来我遇到了问题。

你在使用

setSupportActionBar(bottomAppBar);

连同 replaceMenu?

在这种情况下,删除 setSupportActionBar 一切都会正常。