BottomNavigationView 为空

BottomNavigationView is empty

我正在关注 this tutorialBottomNavigationView 添加到我的 Android Studio 项目。

我将我的项目迁移到 AndroidX 并注意到他在视频中使用的 BottomNavigationView 可能与 AndroidX 不兼容,所以我决定使用 implementation 'com.google.android.material:material:1.1.0' 代替。

问题

添加我的 BottomNavigationView 并设置所有相关属性后,我发现它仍然是空的,尽管遵循了所有说明并检查了一切是否正确。

请注意 BottomNavigationView 是完全空的并且高度为 0

这是我的 activity 的 XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">

    <FrameLayout
        android:id="@+id/frameLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@id/frameLayout"/>

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        app:menu="@menu/bottom_navigation"/>
</RelativeLayout>

这是我的菜单项'XML

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/homeButton"
        android:icon="@android:drawable/ic_menu_agenda"
        android:title="a funny item"/>
    <item
        android:id="@+id/favoritesButton"
        android:icon="@android:drawable/ic_menu_info_details"
        android:title="favorites"/>
    <item
        android:id="@+id/ideaButton"
        android:icon="@android:drawable/ic_menu_agenda"
        android:title="ideas"/>
</menu>

这是怎么回事?我错过了什么?

添加你的依赖项

implementation 'com.google.android.material:material:1.2.0-alpha03'

style.xml

<style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

activity.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <FrameLayout
        android:id="@+id/frameLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@id/frameLayout"
        app:layout_constraintBottom_toTopOf="@+id/bottomNavigationView"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottomNavigationView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:menu="@menu/bottom_navigation" />

</androidx.constraintlayout.widget.ConstraintLayout>

菜单xml文件

<?xml version="1.0" encoding="utf-8"?>
    <menu xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto">
        <item
            android:id="@+id/homeButton"
            android:icon="@android:drawable/ic_menu_agenda"
            android:title="a funny item"/>
        <item
            android:id="@+id/favoritesButton"
            android:icon="@android:drawable/ic_menu_info_details"
            android:title="favorites"/>
        <item
            android:id="@+id/ideaButton"
            android:icon="@android:drawable/ic_menu_agenda"
            android:title="ideas"/>
    </menu>