BottomNavigationView 未显示在模拟器上

时间:2021-07-27 22:31:36

标签: android kotlin bottomnavigationview

我正在尝试在我的 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)
    }
} 

// 截图

enter image description here

enter image description here

更新

我无法让我的应用在设备上运行。是不是因为我在 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)

}

3 个答案:

答案 0 :(得分:1)

您应该在 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)

答案 1 :(得分: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 :(得分:0)

setContentView() 方法中调用 onCreate

相关问题