讲解:BottomNavigationBar是一个第三方的开源控件,BottomNavigationBar就是一个Material风格的底部导航栏
基本使用:
- 在Android Studio下添加依赖关系
compile 'com.ashokvarma.android:bottom-navigation-bar:1.2.0'
- 在布局文件中添加布局
<com.ashokvarma.bottomnavigation.BottomNavigationBar android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:id="@+id/bottom_navigation_bar" />
- 在代码中为BottomNavigationBar添加Item
bottomNavigationBar.addItem(new BottomNavigationItem(R.mipmap.ic_directions_run_white_24dp, "步行")) //参数1:表示为Item添加的图片;参数2:表示为Item添加的文字 .addItem(new BottomNavigationItem(R.mipmap.ic_directions_bike_white_24dp, "骑行")) .addItem(new BottomNavigationItem(R.mipmap.ic_directions_bus_white_24dp, "公交")) .addItem(new BottomNavigationItem(R.mipmap.ic_directions_car_white_24dp, "自驾")) .addItem(new BottomNavigationItem(R.mipmap.ic_directions_railway_white_24dp, "火车")) .initialise();//所有的设置需在调用该方法前完成
- 为选项卡添加事件监听
bottomNavigationBar.setTabSelectedListener(new BottomNavigationBar.OnTabSelectedListener() {//这里也可以使用SimpleOnTabSelectedListener @Override public void onTabSelected(int position) {//未选中 -> 选中 //执行一些必要操作} @Override public void onTabUnselected(int position) {//选中 -> 未选中 } @Override public void onTabReselected(int position) {//选中 -> 选中 } });
其中有几个方法可以为每一个Item设置具体的样式,当他获得焦点时所显示的图片以及字体颜色