2016-03-01 78 views
0

我那里有这样的工具栏和菜单溢出的应用程序..工具栏溢出自定义字体

overflow pic

如何改变工具栏溢出“刷新”的文本字体“设置” 和其他用自定义字体?

+0

可能重复检查这个[link](http://stackoverflow.com/questions/4135699/how-to-set-a-font-for-the-options-menu) –

+0

[Android toolbar中心标题和自定义字体](http ://stackoverflow.com/questions/26533510/android-toolbar -center-title-and-custom-font) –

回答

0

这里是你寻找答案:

public boolean onCreateOptionsMenu(android.view.Menu menu) { 
    MenuInflater inflater = getMenuInflater(); 
    inflater.inflate(R.menu.cool_menu, menu); 
    getLayoutInflater().setFactory(new Factory() { 
     public View onCreateView(String name, Context context, 
       AttributeSet attrs) { 

      if (name.equalsIgnoreCase(
        "com.android.internal.view.menu.IconMenuItemView")) { 
       try { 
        LayoutInflater li = LayoutInflater.from(context); 
        final View view = li.createView(name, null, attrs); 
        new Handler().post(new Runnable() { 
         public void run() { 
          // set the background drawable if you want that 
          //or keep it default -- either an image, border 
          //gradient, drawable, etc. 
          view.setBackgroundResource(R.drawable.myimage); 
          ((TextView) view).setTextSize(20); 

          // set the text color 
          Typeface face = Typeface.createFromAsset(
            getAssets(),"OldeEnglish.ttf");  
          ((TextView) view).setTypeface(face); 
          ((TextView) view).setTextColor(Color.RED); 
         } 
        }); 
        return view; 
       } catch (InflateException e) { 
        //Handle any inflation exception here 
       } catch (ClassNotFoundException e) { 
        //Handle any ClassNotFoundException here 
       } 
      } 
      return null; 
     } 
    }); 
    return super.onCreateOptionsMenu(menu); 
} 

来源:https://stackoverflow.com/a/11376591/5250273

但是,如果你想有一个简单的解决方案,你应该看看这个库:https://github.com/chrisjenx/Calligraphy

+0

它像ActivityBar上的魅力一样工作,但它在工具栏上不起作用。任何想法? –

+0

如果您的工具栏包含TextView等视图,请尝试使用我在帖子中共享的书法库更改它们的字体。 –

+0

图书馆工作...谢谢 –