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,則切換底部按鈕顏色不會切換 } });