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

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

    I know this is an old question, but I came up with a nicer solution: I created this OnTabSelectedListener

    class OnTabSelectedBoldListener : TabLayout.OnTabSelectedListener {
        override fun onTabReselected(tab: TabLayout.Tab) {}
    
        override fun onTabUnselected(tab: TabLayout.Tab) {
            val views = arrayListOf()
            tab.view.findViewsWithText(views, tab.text, View.FIND_VIEWS_WITH_TEXT)
            views.forEach { view ->
                if (view is TextView) {
                    TextViewCompat.setTextAppearance(view, R.style.TabTextAppearance)
                }
            }
        }
    
        override fun onTabSelected(tab: TabLayout.Tab) {
            val views = arrayListOf()
            tab.view.findViewsWithText(views, tab.text, View.FIND_VIEWS_WITH_TEXT)
            views.forEach { view ->
                if (view is TextView) {
                    TextViewCompat.setTextAppearance(view, R.style.TabTextAppearance_Selected)
                }
            }
        }
    }
    

提交回复
热议问题