How to Set a Custom Font in the ActionBar Title?

后端 未结 17 1673
面向向阳花
面向向阳花 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:12

    It's an ugly hack but you can do it like this (since action_bar_title is hidden) :

        try {
            Integer titleId = (Integer) Class.forName("com.android.internal.R$id")
                    .getField("action_bar_title").get(null);
            TextView title = (TextView) getWindow().findViewById(titleId);
            // check for null and manipulate the title as see fit
        } catch (Exception e) {
            Log.e(TAG, "Failed to obtain action bar title reference");
        }
    

    This code is for post-GINGERBREAD devices but this can be easily extended to work with actionbar Sherlock as well

    P.S. Based on @pjv comment there's a better way to find action bar title id

    final int titleId = 
        Resources.getSystem().getIdentifier("action_bar_title", "id", "android");
    

提交回复
热议问题