在 Android 应用程序中,底部导航栏是一种常见的用户界面元素,用于快速导航到应用程序的不同部分。BottomNavigationView 是 Android 支持库中的一个 UI 组件,它提供了一种简单且易于使用的方式来实现底部导航栏。
下面我将详细介绍如何使用 BottomNavigationView 来创建一个具有底部导航栏的 Android 应用程序,并提供相应的源代码示例。
首先,在你的项目中添加以下依赖项,以使用 BottomNavigationView:
implementation 'com.google.android.material:material:1.4.0'
接下来,在你的布局文件中添加 BottomNavigationView,例如,我们创建一个名为 activity_main.xml 的布局文件,并在其中添加一个 BottomNavigationView 和一个用于显示内容的 FrameLayout:
<?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:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/bottomNavigationView" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottomNavigationView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
app:menu