Tablayout找到Tab的TextView(反射)

2021-08-26  本文已影响0人  只会吹水的程序员
fun TabLayout.TabView.getTextView():TextView{
    val tabClass = TabLayout.TabView::class.java
    var declaredField = tabClass.getDeclaredField("textView")
    declaredField.isAccessible=true
    var textView = declaredField.get(this) as TextView
    return textView
}

使用:

     override fun initData() {
        train_tab.setOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
            override fun onTabSelected(tab: TabLayout.Tab) {
                var textView = tab.view.getTextView()
                textView.textSize=24f
                textView.paint.isFakeBoldText=true
            }

            override fun onTabUnselected(tab: TabLayout.Tab) {
                var textView = tab.view.getTextView()
                textView.textSize=16f
                textView.paint.isFakeBoldText=false
            }

            override fun onTabReselected(tab: TabLayout.Tab?) {
            }

        })

    }
上一篇 下一篇

猜你喜欢

热点阅读