首页 > 解决方案 > Android(Xamarin):BottomNavigationView 没有出现在正确的位置

问题描述

我正在尝试使用命名空间中的BottomNavigationViewAndroid.Support.Design.Widget(Xamarin.Android.Support.Design 库,版本 28.0.0.3)为我的 Android 应用程序(API 30)实现底部导航栏。我正在关注这里的解释:https ://devblogs.microsoft.com/xamarin/exploring-androids-bottom-navigation-view/

这是布局文件的样子:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:app="http://schema.android.com/apk/res/res-auto"
                android:layout_width="match_parent"
                android:layout_height="match_parent">
    <FrameLayout android:id="@+id/content"
                 android:layout_width="match_parent"
                 android:layout_height="match_parent"
                 android:layout_above="@+id/navigator"/>
    <android.support.design.widget.BottomNavigationView android:id="@+id/navigator"
                                                        android:layout_width="match_parent"
                                                        android:layout_height="64dp"
                                                        android:layout_gravity="bottom"
                                                        android:background="@android:color/white"
                                                        app:elevation="16dp"
                                                        app:menu="@menu/navigator"/>
</RelativeLayout>

这是菜单文件:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto">
    <item android:id="@+id/home"
          android:icon="@drawable/home"
          android:title="Home"
          android:enabled="true"
          app:showAsAction="ifRoom"/>
    <item android:id="@+id/library"
          android:icon="@drawable/library"
          android:title="Library"
          android:enabled="true"
          app:showAsAction="ifRoom"/>
    <item android:id="@+id/search"
          android:icon="@drawable/search"
          android:title="Search"
          android:enabled="true"
          app:showAsAction="ifRoom"/>
</menu>

问题是,虽然它是可见的,但它出现在屏幕顶部而不是底部,这与底部导航栏应该做的完全相反。此外,它不显示图标。它只是在页面顶部显示为一个空白的白色条。

我做错了什么,我该如何解决?

标签: c#android.netxmlxamarin

解决方案


<com.google.android.material.bottomnavigation.BottomNavigationView
    android:id="@+id/navigation"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="0dp"
    android:layout_marginStart="0dp"
    android:background="?android:attr/windowBackground"
    app:menu="@menu/navigation"
    android:layout_gravity="bottom"
    android:visibility="visible" />

推荐阅读