Android - How to change bottom navigation bar text's font family

后端 未结 11 1518
北荒
北荒 2020-12-14 00:27

I have created a bottom bar navigation in my android page. But now I want to apply the custom font-family in bottom navigation texts.

This is the bottom navigation c

11条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-14 01:26

    Kotlin language:

    1: Create a file with a custom name, ex: BottomNavigationViewExtension.kt

    2: Put the code below:

    import android.graphics.Typeface
    import android.view.View
    import android.view.ViewGroup
    import android.widget.TextView
    import com.google.android.material.bottomnavigation.BottomNavigationView
    
    fun BottomNavigationView.changeNavTypeface(typeface: Typeface) {
        val view: View = this
        checker(view, typeface)
    
    }
    private fun checker(view: View, typeface: Typeface) {
        if (view is ViewGroup) {
            for (i in 0 until view.childCount) {
                val child = view.getChildAt(i)
                checker(child, typeface)
            }
        } else if (view is TextView) {
            view.typeface = typeface
        }
    }
    

    3: Usage:

    navView.changeNavTypeface(
        Typeface.createFromAsset(
            assets,
            "fonts/IRANSansMobile.ttf"
        )
    )
    

提交回复
热议问题