android-fragments 虽然显示了布局预览,但未显示BottomNavigationView

at0kjp5o  于 2022-11-14  发布在  Android
关注(0)|答案(2)|浏览(129)

我打算用三个菜单(每个菜单都有专门的片段)来展示BottomBar。BottomBar没有显示在UI中,尽管在片段下方可以完美地预览相同的菜单。下面粘贴了一个Android Studio的HomeActivity截图。

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

<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)
    }
ssgvzors

ssgvzors1#

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

uqzxnwby

uqzxnwby2#

我认为下面的Stack Overflow问题就是答案(也许,我错了)。我所做的错误是调用起始点是一个片段的导航图。相反,我现在直接使用startActivity调用Activity。
我对解决办法没有把握;我的结论是,虽然不满意。


相关问题