【问题标题】:BottomNavigationView is not show though layout preview shows尽管显示布局预览,但未显示 BottomNavigationView
【发布时间】:2019-10-10 18:10:56
【问题描述】:

旨在显示带有 3 个菜单的底部栏(每个菜单都有专用片段)。尽管相同的预览完全位于片段下方,但 BottomBar 不会显示在 UI 中。贴在下面的 HomeActivity 的 AndroidStudio 屏幕截图。

ConstraintLayout 是否有任何限制,因为大多数示例都使用 CoordinatedLayout?

<androidx.constraintlayout.widget.ConstraintLayout 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:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".home.HomeActivity">

    <fragment
        android:id="@+id/nav_host_home_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:defaultNavHost="true"
        app:layout_constraintBottom_toTopOf="@id/bottomNavigationView"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:navGraph="@navigation/nav_home" />

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottomNavigationView"
        style="@style/Widget.MaterialComponents.BottomNavigationView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?android:attr/windowBackground"
        app:itemIconTint="@color/colorPrimary"
        app:itemTextColor="@color/colorPrimary"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:menu="@menu/bottom_home_menu" />

</androidx.constraintlayout.widget.ConstraintLayout>

在 HomeActivity 中设置 BottomBar 如下:

    private fun setupNavigation() {
        val navController = findNavController(R.id.nav_host_home_fragment)
        // Passing each menu ID as a set of Ids because each
        // menu should be considered as top level destinations.
        val appBarConfiguration = AppBarConfiguration(
            setOf(
                R.id.navigation_home, R.id.navigation_bookings, R.id.navigation_profile
            )
        )
        setupActionBarWithNavController(navController, appBarConfiguration)
        bottomNavigationView.setupWithNavController(navController)
    }

【问题讨论】:

    标签: android android-fragments android-architecture-navigation android-bottom-nav-view


    【解决方案1】:

    在您的 XML 文件中,在底部导航视图中, 将 android:layout_width="0dp" 更改为 android:layout_width="wrap_content"

    【讨论】:

    • 谢谢,但没有帮助!我怀疑使用 Jetpack 的导航组件并调用 nav_host_home_fragment 而不是活动布局。 bottomNavBar 在 Activity 布局中。
    【解决方案2】:

    考虑下面的讨论作为答案(也许,我错了)。我犯的错误是调用以片段为起点的导航图。相反,现在直接使用 startActivity 调用 Activity。

    不确定解决方案,虽然不满意,但我还是得出结论。

    Android Navigation Component navigate between graphs

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多