Android BottomNavigationView+Novigation实现底部导航功能

实现效果在这里插入图片描述

1.创建nav_graph.xml

右键res / NEW / Android Resource File创建类型为Navigation的xml文件.

<?xml version="1.0" encoding="utf-8"?>
<navigation 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/nav_graph">
</navigation>
2.创建对应的4个Fragment

打开刚才创建的nav_graph.xml,右上角切换位Design视图,点击New Destination=>Create new destination ,创建对应的Fragment.
在这里插入图片描述

<?xml version="1.0" encoding="utf-8"?>
<navigation 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/nav_graph"
    app:startDestination="@id/homeFragment">

    <fragment
        android:id="@+id/homeFragment"
        android:name="com.moyihen.smartoa.ui.home.HomeFragment"
        android:label="fragment_home"
        tools:layout="@layout/fragment_home" />
    <fragment
        android:id="@+id/recordFragment"
        android:name="com.moyihen.smartoa.RecordFragment"
        android:label="fragment_record"
        tools:layout="@layout/fragment_record" />
    <fragment
        android:id="@+id/clockFragment"
        android:name="com.moyihen.smartoa.ClockFragment"
        android:label="fragment_clock"
        tools:layout="@layout/fragment_clock" />
    <fragment
        android:id="@+id/myFragment"
        android:name="com.moyihen.smartoa.MyFragment"
        android:label="fragment_my"
        tools:layout="@layout/fragment_my" />
</navigation>
3.创建BottomNavigationView需要的navigation_menu.xml

右键res / NEW / Android Resource File创建类型为Menu的navigation_menu.xml文件.

注意:这边的item Id 要和上面的nav_graph.xml fragment里面的id一致

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/homeFragment"
        android:icon="@drawable/nav_btn_1"
        android:title="首页" />
    <item
        android:id="@+id/recordFragment"
        android:icon="@drawable/nav_btn_2"
        android:title="记录" />
    <item
        android:id="@+id/clockFragment"
        android:icon="@drawable/nav_btn_3"
        android:title="定时" />
    <item
        android:id="@+id/myFragment"
        android:icon="@drawable/nav_btn_3"
        android:title="我的" />
</menu>
4.创建MainActivity

activity_main.xml摆放BottomNavigationView和fragment
注意:底部导航按钮超过3个会不显示文字,需要加上 app:labelVisibilityMode=“labeled”

<?xml version="1.0" encoding="utf-8"?>
<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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/nav_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:labelVisibilityMode="labeled"
        app:menu="@menu/navigation_menu" />

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

</androidx.constraintlayout.widget.ConstraintLayout>
class MainActivity : AppCompatActivity() {
    
    

    private lateinit var binding: ActivityMainBinding

    override fun onCreate(savedInstanceState: Bundle?) {
    
    
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)


        val navView = binding.navView

        val navController = findNavController(R.id.nav_host_fragment_activity_main)
        val appBarConfiguration = AppBarConfiguration(
            setOf(
                R.id.homeFragment,
                R.id.recordFragment,
                R.id.clockFragment,
                R.id.myFragment
            )
        )
        //App bar与NavController绑定 动态改变title
        setupActionBarWithNavController(navController,appBarConfiguration)

        navView.setupWithNavController(navController)

    }
}

end

猜你喜欢

转载自blog.csdn.net/qq_35193677/article/details/124750384