美文网首页
Tablayout找到Tab的TextView(反射)

Tablayout找到Tab的TextView(反射)

作者: 只会吹水的程序员 | 来源:发表于2021-08-26 17:03 被阅读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?) {
            }

        })

    }

相关文章

网友评论

      本文标题:Tablayout找到Tab的TextView(反射)

      本文链接:https://www.haomeiwen.com/subject/hswmiltx.html