首页 > 解决方案 > BottomNavigationView 没有膨胀

问题描述

我正在尝试为 BottomNavigationView 充气,activity_main.xml但无济于事。

这是我bottom_navigation.xmlmenu资源目录中的:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:id="nav_library"
        android:icon="@drawable/svg_library"
        android:title="Library" />

    <item
        android:id="nav_search"
        android:icon="@drawable/svg_search"
        android:title="Search" />

    <item
        android:id="nav_liked"
        android:icon="@drawable/svg_heart"
        android:title="Liked" />

</menu>

这是我的activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/nav_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        app:menu="@menu/bottom_navigation"
        android:background="?android:attr/windowBackground" />

</RelativeLayout>

似乎android:layout_height="wrap_content"没有检测到菜单的图标和标题,因此导航栏的高度和宽度都是 0。

这是带有wrap_content属性的图像。(没有足够的代表进行嵌入)

请指教。

PS 这是我关于 Stack Overflow 的第一个问题,所以我的问题格式可能不是最好的。

标签: androidxml

解决方案


派对有点晚了。但那天我终于想通了。缺少前缀@+id/

    <item
        android:id="@+id/nav_library"
        android:icon="@drawable/svg_library"
        android:title="Library" />

推荐阅读