How to Set a Custom Font in the ActionBar Title?

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

    Following code will work for all the versions. I did checked this in a device with gingerbread as well as on JellyBean device

     private void actionBarIdForAll()
        {
            int titleId = 0;
    
            if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.HONEYCOMB)
            {
                titleId = getResources().getIdentifier("action_bar_title", "id", "android");
            }
            else
            {
              // This is the id is from your app's generated R class when ActionBarActivity is used for SupportActionBar
    
                titleId = R.id.action_bar_title;
            }
    
            if(titleId>0)
            {
                // Do whatever you want ? It will work for all the versions.
    
                // 1. Customize your fonts
                // 2. Infact, customize your whole title TextView
    
                TextView titleView = (TextView)findViewById(titleId);
                titleView.setText("RedoApp");
                titleView.setTextColor(Color.CYAN);
            }
        }
    

提交回复
热议问题