How to change selected Tab Text color using TabLayout from code in Android?

后端 未结 8 939
悲哀的现实
悲哀的现实 2020-12-13 01:58

\"enter

I\'m using android.support.widget.TabLayout

8条回答
  •  忘掉有多难
    2020-12-13 02:19

    Please check out following answer

     tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                View view = tab.getCustomView();
                RelativeLayout relativeLayout = (RelativeLayout) view.findViewById(R.id.layout_background);
                relativeLayout.setBackgroundColor(ContextCompat.getColor(getActivity(), R.color.white));
                TypefacedTextView selectedText = (TypefacedTextView) view.findViewById(R.id.txt_tab_name);
                selectedText.setTextColor(ContextCompat.getColor(getActivity(), R.color.colorPrimary));
    
            }
    
            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                View view = tab.getCustomView();
                RelativeLayout relativeLayout = (RelativeLayout) view.findViewById(R.id.layout_background);
                relativeLayout.setBackgroundColor(ContextCompat.getColor(getActivity(), R.color.colorPrimary));
                TypefacedTextView selectedText = (TypefacedTextView) view.findViewById(R.id.txt_tab_name);
                selectedText.setTextColor(ContextCompat.getColor(getActivity(), R.color.white));
            }
    
            @Override
            public void onTabReselected(TabLayout.Tab tab) {
    
            }
        });
    

    where tabLayout is object of TabLayout Class

提交回复
热议问题