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

后端 未结 11 2847
南笙
南笙 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:48

    This works in JAVA

    tabs.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            initTabSelection(tab);
    
            for(int index = 0; index < ((ViewGroup) tab.view).getChildCount(); index++) {
                View nextChild = ((ViewGroup) tab.view).getChildAt(index);
                if (nextChild instanceof TextView) {
                    TextView v = (TextView) nextChild;
                    v.setTypeface(null, Typeface.BOLD);
                }
            }
        }
    
        @Override
        public void onTabUnselected(TabLayout.Tab tab) {
            for(int index = 0; index < ((ViewGroup) tab.view).getChildCount(); index++) {
                View nextChild = ((ViewGroup) tab.view).getChildAt(index);
                if (nextChild instanceof TextView) {
                    TextView v = (TextView) nextChild;
                    v.setTypeface(null, Typeface.NORMAL);
                }
            }
        }
    
        @Override
        public void onTabReselected(TabLayout.Tab tab) { }
    });
    

提交回复
热议问题