【问题标题】:Why my bottom navigation bar doesn't appear为什么我的底部导航栏没有出现
【发布时间】:2020-05-03 12:01:11
【问题描述】:

我设置了底部导航栏,但不知何故在我的屏幕上看不到它。

我的设置过程:

xml

<androidx.appcompat.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">

<androidx.appcompat.widget.Toolbar
    android:id="@+id/my_toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    android:elevation="24dp"
    android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>

<fragment
    android:id="@+id/nav_host_fragment"
    android:name="androidx.navigation.fragment.NavHostFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:defaultNavHost="true"
    app:navGraph="@navigation/nav_graph"
 />
<com.google.android.material.bottomnavigation.BottomNavigationView
    android:id="@+id/bottom_nav_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:menu="@menu/bottom_nav_menu"
    />

</androidx.appcompat.widget.LinearLayoutCompat>

菜单

<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/mainFragment"
        android:icon="@drawable/ic_launcher_background"
        android:title="@string/home" />
    <item
        android:id="@+id/newsList"
        android:icon="@drawable/ic_launcher_background"
        android:title="@string/news_list" />
    <item
        android:id="@+id/settings"
        android:icon="@drawable/ic_launcher_background"
        android:title="@string/settings" />
</menu>

MainActivity

bottom_nav_view.setupWithNavController(navController)

我哪里做错了?我查了一些教程,这正是设置底部导航视图的过程

【问题讨论】:

  • 为项目设置此属性:app:showAsAction="always"
  • 仍然没有出现..即使在主activity.xml设计选项卡中也看不到它

标签: android android-layout bottomnavigationview


【解决方案1】:

使用这个 heightweight 作为您的片段标签:

<fragment
    android:id="@+id/nav_host_fragment"
    android:name="androidx.navigation.fragment.NavHostFragment"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    app:defaultNavHost="true"
    app:navGraph="@navigation/nav_graph"
 />

【讨论】:

  • 工作,谢谢。只是一般性问题 - 如果匹配父高度是问题,为什么我可以看到应用栏并且片段也没有重叠?
  • 当您使用match_parent 作为LinearLayout 中的布局尺寸时,视图会与包含父级 (LinearLayout) 一样多,在这种情况下这不是您的意图。您宁愿填充其他视图(工具栏和底部导航)之间的剩余空间,您应该使用这种方式声明它。工具栏可见的原因是它的声明顺序是第一个,并且您的片段被推到视野之外?attr/actionBarSize 像素。
【解决方案2】:

根据我的代码修改你的代码

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/my_toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:layout_alignParentTop="true"
        android:background="?attr/colorPrimary"
        android:elevation="24dp"
        android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

    <fragment
        android:id="@+id/nav_host_fragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:defaultNavHost="true"
        android:layout_below="@id/my_toolbar"
        android:layout_above="@id/bottom_nav_view"
        app:navGraph="@navigation/nav_graph" />

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottom_nav_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        app:menu="@menu/bottom_nav_menu" />

</RelativeLayout>

此代码经过我自己的测试并且可以正常工作

谢谢!快乐编码

【讨论】:

    猜你喜欢
    • 2022-11-26
    • 2020-05-17
    • 1970-01-01
    • 2020-02-07
    • 1970-01-01
    • 1970-01-01
    • 2020-03-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多