【问题标题】:BottomNavigationView Isn't Showing on EmulatorBottomNavigationView 未在模拟器上显示
【发布时间】:2021-07-27 22:31:36
【问题描述】:

我试图在我的 MainActivity 上设置一个 BottomNavigationView,但它只显示在我的 activity_main.xml 而不是模拟器中。我应该在我的 MainActivity.kt 中设置一些东西以使其在模拟器上可见吗?谢谢!

//activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingTop="?attr/actionBarSize">
    <androidx.fragment.app.FragmentContainerView
        android:id="@+id/nav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/nav_view"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true"
        android:layout_marginStart="0dp"
        android:layout_marginTop="0dp"
        android:layout_marginEnd="0dp"
        android:layout_marginBottom="0dp"
        app:defaultNavHost="true"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/nav_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_marginStart="0dp"
        android:layout_marginEnd="0dp"
        android:background="?android:attr/windowBackground"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:menu="@menu/bottom_nav_menu" >
    </com.google.android.material.bottomnavigation.BottomNavigationView>
</RelativeLayout>

// MainActivity.kt

class MainActivity : AppCompatActivity() {

    private lateinit var binding: ActivityMainBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    }
} 

// 截图

更新

我无法让我的应用在设备上运行。是因为我在 OnCreate 中设置了错误吗?

// MainActivity.kt

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val bottomNavigationView = findViewById<BottomNavigationView>(R.id.nav_view)
        val navController = findNavController(R.id.nav_host_fragment)
        val appBarConfiguration = AppBarConfiguration(setOf(R.layout.fragment_news_feed, R.layout.fragment_friends, R.layout.fragment_messaging, R.layout.fragment_notifications, R.layout.fragment_menu))
        setupActionBarWithNavController(navController, appBarConfiguration)
        bottomNavigationView.setupWithNavController(navController)

}

【问题讨论】:

    标签: android kotlin bottomnavigationview


    【解决方案1】:

    试试这个。对我有用。

    setContentView(R.layout.activity_main);
    
    class MainActivity : AppCompatActivity() {
    
        private lateinit var binding: ActivityMainBinding
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_main);
        }
    } 
    

    【讨论】:

      【解决方案2】:

      您应该在 onCrate 中设置菜单。 示例:

      val bottomNavigationView = findViewById<BottomNavigationView>(R.id.id_ your_bottomNavigationView)
      val navController = findNavController(R.id.your_fragment)
      val appBarConfiguration = AppBarConfiguration(setOf(R.id.your_fragment1, R.id.your_fragment2, R.id.your_fragment3, R.id.your_fragment4))
      setupActionBarWithNavController(navController, appBarConfiguration)
      bottomNavigationView.setupWithNavController(navController)
      

      【讨论】:

      • 一切都需要碎片化吗,还是我的导航项现在足够好?
      • 菜单中的每个视图都需要片段。 “your_fragment”是 MainActivity xml 中的主要片段,“your_fragment1 ... your_fragment4”是菜单中每个按钮的视图。
      【解决方案3】:

      onCreate 方法中调用setContentView()

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-06-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多