How to change ActionBar title font when using AppCompat

后端 未结 3 789
时光取名叫无心
时光取名叫无心 2021-02-02 00:06

I would like to apply custom font to the title of my app which is displayed on the ActionBar. Previously I didn\'t use any support library and this solution:

int         


        
3条回答
  •  北恋
    北恋 (楼主)
    2021-02-02 00:18

    Assuming you are already successfully using the new toolbar of AppCompat v22+ and that you are importing android.support.v7.widget.Toolbar in your activity.

    So on your OnCreate you should already have

    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    

    at this point you are almost done:

    import java.lang.reflect.field;

    and add following lines:

    TextView yourTextView = null;
    try {
        Field f = toolbar.getClass().getDeclaredField("mTitleTextView");
        f.setAccessible(true);
        yourTextView = (TextView) f.get(toolbar);
        yourTextView.setTypeface(face);
    } catch (NoSuchFieldException e) {
    } catch (IllegalAccessException e) {
    }
    

提交回复
热议问题