item selected color in android BottomNavigationView

前端 未结 6 1139
别那么骄傲
别那么骄傲 2021-02-01 11:58

I refer this. Schedules Activity is appeared when I click Schedules, but first item color (Favorites) is always selected. It doesn\'t change Schedules item color from Favorites

6条回答
  •  挽巷
    挽巷 (楼主)
    2021-02-01 12:41

    Have you heard about the wrapper project called BottomBar of Roughike which makes the use of BottomNavigationView easier? Project can be found here.

    I suggest you to use this project which is up to date and has contribution in a high level. If you refer to use this, You can simply insert the below code to change the colors when clicked on tabs and do much more customized stuff:

    TabLayout tabLayout = (TabLayout) findViewById(R.id.tabLayout);
    Tab tab = newTab().setIcon(new BitmapDrawable(getResources(), icon)));
    tab.getIcon().setColorFilter(Color.parseColor("#7E7E7E"), PorterDuff.Mode.SRC_IN);
    
    tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
                              @Override public void onTabSelected(TabLayout.Tab tab) {
                                if (tab != null && tab.getIcon() != null) {
                                  tab.getIcon().clearColorFilter();
                                  }
                              }
                              @Override public void onTabUnselected(TabLayout.Tab tab) {
                                if (tab != null && tab.getIcon() != null) {
                                  tab.getIcon()
                                      .setColorFilter(Color.parseColor("#7E7E7E"),
                                          PorterDuff.Mode.SRC_IN);
                                }
                              }
                              @Override public void onTabReselected(TabLayout.Tab tab) {
                              }
                            });
                          }
                        }
                      });
    

    So basically what I do here, I color unselected tabs to #7E7E7E and clear the filter for coloring from selected ones so they appear with their original color of their icon. Of course, you can fill with another color when selected as well, that's up to you.

    Hope this helps you!

    Cheers,

    Renc

提交回复
热议问题