How to Set a Custom Font in the ActionBar Title?

后端 未结 17 1517
面向向阳花
面向向阳花 2020-11-22 09:46

How (if possible) could I set a custom font in a ActionBar title text(only - not the tab text) with a font in my assets folder? I don\'t want to use the android:logo option.

17条回答
  •  醉酒成梦
    2020-11-22 10:18

    We need to use reflections for achieving this

    final int titleId = activity.getResources().getIdentifier("action_bar_title", "id", "android");
    
        final TextView title;
        if (activity.findViewById(titleId) != null) {
            title = (TextView) activity.findViewById(titleId);
            title.setTextColor(Color.BLACK);
            title.setTextColor(configs().getColor(ColorKey.GENERAL_TEXT));
            title.setTypeface(configs().getTypeface());
        } else {
            try {
                Field f = bar.getClass().getDeclaredField("mTitleTextView");
                f.setAccessible(true);
                title = (TextView) f.get(bar);
                title.setTextColor(Color.BLACK);
                title.setTypeface(configs().getTypeface());
            } catch (NoSuchFieldException e) {
            } catch (IllegalAccessException e) {
            }
        }
    

提交回复
热议问题