1、xml
<!--app:labelVisibilityMode="labeled" --> <com.google.android.material.bottomnavigation.BottomNavigationView android:id="@+id/bottom_view" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="bottom" android:background="@android:color/white" app:labelVisibilityMode="labeled" app:layout_behavior=".utils.BottomNavigationBehavior" design:menu="@menu/tab_menu" />
2、java
//onCreate中 viewDataBinding.bottomView.setLabelVisibilityMode(LabelVisibilityMode.LABEL_VISIBILITY_LABELED);
3、底部组建切换
viewDataBinding.bottomView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() { @Override public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) { Fragment fragment = null; switch (menuItem.getItemId()) { case R.id.menu: fragment = mHomeFragment; break; } if (getSupportActionBar() != null) { getSupportActionBar().setTitle(menuItem.getTitle());//如果toolbar不为空,将title换掉 } switchFragment(fromFragment, fragment); fromFragment = fragment; return true; //如果返回为false,则切换底部按钮颜色不会切换 } });