achieve effect
1. Create nav_graph.xml
Right-click res / NEW / Android Resource File to create an xml file of type Navigation.
<?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. Create the corresponding 4 Fragments
Open the nav_graph.xml just created, switch to the Design view in the upper right corner, click New Destination=>Create new destination, and create the corresponding 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. Create the navigation_menu.xml required by BottomNavigationView
Right-click res / NEW / Android Resource File to create a navigation_menu.xml file of type Menu.
Note: The item Id here should be consistent with the id in the above nav_graph.xml fragment
<?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. Create MainActivity
Activity_main.xml places BottomNavigationView and fragment
Note: If there are more than 3 bottom navigation buttons, the text will not be displayed, and app:labelVisibilityMode="labeled" needs to be added
<?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