Android BottomNavigationView为空
我在下面为我的Android Studio项目添加了Android BottomNavigationView为空,android,gradle,bottomnavigationview,Android,Gradle,Bottomnavigationview,我在下面为我的Android Studio项目添加了BottomNavigationView 我将我的项目迁移到AndroidX,并注意到他在视频中使用的BottomNavigationView可能与AndroidX不兼容,因此我决定改用implementation'com.google.android.material:material:1.1.0' 问题 添加我的BottomNavigationView并设置所有相关属性后,我发现它仍然是空的,尽管遵循所有说明并检查所有内容是否正确 请注意B
BottomNavigationView
我将我的项目迁移到AndroidX,并注意到他在视频中使用的BottomNavigationView
可能与AndroidX不兼容,因此我决定改用implementation'com.google.android.material:material:1.1.0'
问题
添加我的BottomNavigationView
并设置所有相关属性后,我发现它仍然是空的,尽管遵循所有说明并检查所有内容是否正确
请注意BottomNavigationView是如何完全为空且高度为0的
这是我的活动的XML:
这是我菜单项的XML
这里怎么了?我错过了什么?
添加您的依赖项
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>
添加您的依赖项
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>
复制并粘贴所有内容,重新构建项目,现在可以正常工作了。但愿我能知道为什么它不起作用。Android Studio有时使用起来非常令人沮丧。复制并粘贴了所有内容,重建了项目,现在它可以工作了。但愿我能知道为什么它不起作用。Android Studio有时使用起来非常令人沮丧。