首页 > 解决方案 > 更改高度和颜色选项卡指示器

问题描述

我正在尝试在自定义选项卡布局中更改棕褐色指示器的高度和颜色,但我无法做到。这是我通常在我的应用程序中使用的自定义组件。

我正在尝试通过代码更改高度和颜色,我知道从 xml 中我可以做到这一点。

这是我到目前为止所拥有的:

 class CustomTabLayout : TabLayout {

private var mTypeface: Typeface? = null


constructor(context: Context) : super(context) {
    init()
}

constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {
    init()
}

constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(
    context,
    attrs,
    defStyleAttr
) {
    init()
}

private fun init() {
    mTypeface = ResourcesCompat.getFont(context, R.font.montserrat_medium)
}

override fun addTab(tab: Tab, position: Int, setSelected: Boolean) {
    super.addTab(tab, position, setSelected)
    val mainView = getChildAt(0) as ViewGroup
    val tabView = mainView.getChildAt(tab.position) as ViewGroup
    val tabChildCount = tabView.childCount
    for (i in 0 until tabChildCount) {
        val tabViewChild = tabView.getChildAt(i)
        if (tabViewChild is TextView) {
            tabViewChild.setTypeface(mTypeface, Typeface.NORMAL)
            tabViewChild.isAllCaps = false
        }
    }
}

  }

任何帮助将不胜感激。

标签: kotlintabsandroid-tablayout

解决方案


推荐阅读