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

后端 未结 11 1492
北荒
北荒 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:21

    If you have a CustomFont in "Asset Folder" and you want to set in your "Bottom Navigation" use this code

            public static void persian_iran_font(final Context context, final View v) {
                try {
                    if (v instanceof ViewGroup) {
                        ViewGroup vg = (ViewGroup) v;
                        for (int i = 0; i < vg.getChildCount(); i++) {
                            View child = vg.getChildAt(i);
                            persian_iran_font(context, child);
                        }
                    } else if (v instanceof TextView) {
                        ((TextView) v).setTypeface(Typeface.createFromAsset(context.getAssets(), "teshrinarmedium.otf"));
                    }
                } catch (Exception e) {
                }
            }
        
    

    And then use methode in your MainActivity Like This

      BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
      persian_iran_font(getApplicationContext(), navigation);
    

    kotlin Version

    fun persian_iran_font(context: Context, v: View) {
        try {
            if (v is ViewGroup) {
                val vg = v as ViewGroup
                for (i in 0 until vg.childCount) {
                    val child: View = vg.getChildAt(i)
                    persian_iran_font(context, child)
                }
            } else if (v is TextView) {
                (v as TextView).setTypeface(
                    Typeface.createFromAsset(
                        context.getAssets(),
                        "teshrinarmedium.otf"
                    )
                )
            }
        } catch (e: Exception) {
        }
    }
    

    Goodluck

提交回复
热议问题