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

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

    In addition to previous answers, bear in mind that when modifying the style of the text in the onTabUnselected you may have to recalculate the width of the view if it was set on WRAP_CONTENT

        @Override
        public void onTabSelected(TabLayout.Tab tab) {
                TextView text = (TextView) tab.getCustomView();
                text.setTypeface(null, Typeface.BOLD); 
                text.getCustomView().measure(WRAP_CONTENT, WRAP_CONTENT)
                text.getCustomView().layoutParams.height = measuredHeight
                text.getCustomView().layoutParams.width = measuredWidth
        }
    

提交回复
热议问题