我跟随this tutorial在我的Android Studio项目中添加了一个BottomNavigationView。
我将我的项目迁移到AndroidX上,注意到他在视频中使用的BottomNavigationView可能与AndroidX不兼容,所以我决定使用implementation 'com.google.android.material:material:1.1.0'。
问题所在
添加我的BottomNavigationView并设置所有相关属性后,我看到它仍然是空的,尽管遵循所有说明并检查一切都是正确的。
请注意BottomNavigationView是如何完全为空的,并且高度为0

这是我的活动的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>这是怎么回事?我遗漏了什么?
发布于 2020-03-09 19:36:51

添加您的依赖项
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>https://stackoverflow.com/questions/60599633
复制相似问题