这篇文章主要介绍通过Fragement + BottomNavigationView实现Android应用底部的导航栏,效果如下图
关于fragment使用的详细介绍,大家可以点击这里 。
下面进入正文,
-
首先在Android视图下,打开Gradle Scripts 下的build gradle(Module:app),添加如下依赖,注意后面的版本号要与你项目中已有的依赖版本一致,这里是28.0.0。
implementation 'com.android.support:design:28.0.0' implementation 'com.android.support:support-vector-drawable:28.0.0'
-
在res下新建一个文件夹,命名为menu(如果有就不用新建了),在menu文件夹下新建一个菜单文件用于底部导航栏显示的内容,这里命名为
navigation.xml
,代码如下,其中icon属性表示的是显示的图标,大家可以自行设置图标路径
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/home_page"
android:icon="@drawable/show"
android:title="首页" />
<item
android:id="@+id/setting"
android:icon="@drawable/setting"
android:title="设置" />
<item
android:id="@+id/mycenter"
android:icon="@drawable/footer_btn_mycenter_preesed"
android:title="个人中心" />
</menu>
- 新建一个布局文件,用于对设置整体页面布局,命名为
start_for_fragment.xml
代码如下
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context=".startForFramment">
<LinearLayout
andr