1.Activity页面
XML
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#EFEDED"
android:fitsSystemWindows="false"
>
<FrameLayout
android:id="@+id/frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/bottom_navigation_1" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_navigation_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:itemBackground="@color/white" 导航栏背景颜色
app:itemIconTint="@color/selector_color" icon图片选中和未选中颜色
app:itemTextColor="@color/selector_color" 文字选中和未选中颜色
app:menu="@menu/bottom_navigation_menu"
app:labelVisibilityMode="labeled"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
2.创建menu页面 (将需要的图片放入mipmap中或drawable中)
XML
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/navigation_home"
android:icon="@drawable/home"
android:title="首页"
android:checked="true"/>
<item
android:id="@+id/navigation_quanzi"
android:icon="@drawable/jiedan_icon"
android:title="接单" />
<item
android:id="@+id/navigation_dingdan"
android:icon="@drawable/dingdan"
android:title="订单" />
<item
android:id="@+id/navigation_mine"
android:icon="@drawable/mine"
android:title="我的" />
</menu>
3.在Activity的java文件中填写 点击事件(切换fragment页面)
java
private BottomNavigationView bottomNavigationView;
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_home:
ReplaceFragment(new HomeFragment());
return true;
case R.id.navigation_quanzi:
ReplaceFragment(new QuanZiFragment());
return true;
case R.id.navigation_dingdan:
ReplaceFragment(new DingDanFragment());
return true;
case R.id.navigation_mine:
ReplaceFragment(new MineFragment());
return true;
}
return false;
}
});
创建调用切换页面的方法
java
private void ReplaceFragment(Fragment fragment){
FragmentManager fragmentManager=getSupportFragmentManager();
FragmentTransaction transaction= fragmentManager.beginTransaction();
transaction.replace(R.id.frame,fragment);
transaction.commit();
}