How do I change the text style of a selected tab when using tabLayout?

后端 未结 11 3129
南笙
南笙 2021-02-19 07:57

I want to make the text of a selected tab bold. How can I do this either through xml or java code, whatever is easier.

11条回答
  •  没有蜡笔的小新
    2021-02-19 08:52

           tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                tab.getCustomView().setBackgroundDrawable(getResources().getDrawable(R.mipmap.account_active));
              if (tab.getPosition()==0){
                  tab.getCustomView().setBackgroundDrawable(getResources().getDrawable(R.mipmap.booking_active));
              }
              if(tab.getPosition()==1){
                  tab.getCustomView().setBackgroundDrawable(getResources().getDrawable(R.mipmap.account_active));
              }
            }
    
            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
    
                if (tab.getPosition()==0){
                    tab.getCustomView().setBackgroundDrawable(getResources().getDrawable(R.mipmap.booking_deacive));
                }
                if(tab.getPosition()==1){
                    tab.getCustomView().setBackgroundDrawable(getResources().getDrawable(R.mipmap.account_deactive));
                }
            }
    
            @Override
            public void onTabReselected(TabLayout.Tab tab) {
    
                if (tab.getPosition()==0){
                    tab.getCustomView().setBackgroundDrawable(getResources().getDrawable(R.mipmap.booking_active));
                }
                if(tab.getPosition()==1){
                    tab.getCustomView().setBackgroundDrawable(getResources().getDrawable(R.mipmap.account_active));
                }
            }
        });
    
        setupTabIcons();
    

提交回复
热议问题