Change the font of tab text in android design support TabLayout

前端 未结 18 1734
执念已碎
执念已碎 2020-11-27 13:38

I\'m trying to work on the new TabLayout from the android design library.

I want to change tab text to custom font. And,I tried to sear

18条回答
  •  执念已碎
    2020-11-27 14:22

    And here is my implementation in Kotlin that also allow change font for selected and unselected tabs.

    class FontTabLayout @JvmOverloads constructor(
        context: Context,
        attrs: AttributeSet? = null,
        @AttrRes defStyleAttr: Int = 0
    ) : TabLayout(context, attrs, defStyleAttr) {
    
        private var textSize = 14f
    
        private var defaultSelectedPosition = 0
    
        private var selectedTypeFace: Typeface? = ResourcesCompat.getFont(context, R.font.muli_bold)
        private var normalTypeFace: Typeface? = ResourcesCompat.getFont(context, R.font.muli_regular)
    
        @ColorInt private var selectedColor = 0
        @ColorInt private var normalTextColor = 0
    
        init {
            attrs?.let { initAttrs(it) }
            addOnTabSelectedListener()
        }
    
        private fun initAttrs(attrs: AttributeSet) {
            val a = context.obtainStyledAttributes(attrs, R.styleable.FontTabLayout)
    
            textSize = a.getDimensionPixelSize(R.styleable.FontTabLayout_textSize, 14).toFloat()
    
            defaultSelectedPosition = a.getInteger(R.styleable.FontTabLayout_defaultSelectedPosition, 0)
            val selectedResourceId = a.getResourceId(R.styleable.FontTabLayout_selectedTypeFace, R.font.muli_bold)
            val normalResourceId = a.getResourceId(R.styleable.FontTabLayout_normalTypeFace, R.font.muli_regular)
    
            selectedColor = a.getColor(com.google.android.material.R.styleable.TabLayout_tabSelectedTextColor, 0)
            normalTextColor = a.getColor(R.styleable.FontTabLayout_normalTextColor, 0)
    
            selectedTypeFace = ResourcesCompat.getFont(context, selectedResourceId)
            normalTypeFace = ResourcesCompat.getFont(context, normalResourceId)
    
            a.recycle()
        }
    
        private fun addOnTabSelectedListener() {
            addOnTabSelectedListener(object : OnTabSelectedListenerAdapter() {
    
                override fun onTabUnselected(tab: Tab?) {
                    getCustomViewFromTab(tab)?.apply {
                        setTextColor(normalTextColor)
                        typeface = normalTypeFace
                    }
                }
    
                override fun onTabSelected(tab: Tab?) {
    
                    getCustomViewFromTab(tab)?.apply {
                        setTextColor(selectedColor)
                        typeface = selectedTypeFace
                    }
                }
    
                private fun getCustomViewFromTab(tab: Tab?) = tab?.customView as? AppCompatTextView
    
            })
        }
    
        override fun setupWithViewPager(viewPager: ViewPager?, autoRefresh: Boolean) {
            super.setupWithViewPager(viewPager, autoRefresh)
            addViews(viewPager)
        }
    
        private fun addViews(viewPager: ViewPager?) {
            for (i in 0 until tabCount) {
                val customTabView = getCustomTabView(i).apply {
                    typeface = if (i == defaultSelectedPosition) selectedTypeFace else normalTypeFace
                    val color = if (i == defaultSelectedPosition) selectedColor else normalTextColor
                    setTextColor(color)
                    text = viewPager?.adapter?.getPageTitle(i)
                }
    
                getTabAt(i)?.customView = customTabView
            }
        }
    
        private fun getCustomTabView(position: Int): AppCompatTextView {
            return AppCompatTextView(context).apply {
                gravity = Gravity.CENTER
                textSize = this@FontTabLayout.textSize
                text = position.toString()
            }
        }
    }
    

    in attrs.xml:

    
        
        
        
        
        
    
    

提交回复
热议问题