在android中更改应用程序标题的字体

我有一个字体,我想在android中更改动作栏标题的字体。 有没有办法像这样设置标题字体?

this.setTitle(myTitle.toUpperCase()); this.setTypefaceofTitle(tf); 

这不是一个复制问题,这个链接上的方法( 如何在ActionBar标题中设置自定义字体? )不起作用。 当我尝试它们时,eclipse会给出错误: java.lang.noSuchMethodError

尝试这个,

  int actionBarTitle = Resources.getSystem().getIdentifier("action_bar_title", "id", "android"); TextView actionBarTitleView = (TextView) getWindow().findViewById(actionBarTitle); Typeface robotoBoldCondensedItalic = Typeface.createFromAsset(getAssets(), "fonts/Roboto-BoldCondensedItalic.ttf"); if(actionBarTitleView != null){ actionBarTitleView.setTypeface(robotoBoldCondensedItalic); } 

如果它的工具栏意味着尝试如下。

  
		      	

我从headsvk找到了这个帮助我的答案 。 做一些调整我只需要这样做:

 Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); ... TextView myTitle = (TextView) toolbar.getChildAt(0); myTitle.setTypeface(font); 

在我的应用程序上做一些测试我发现标题TextView是工具栏的第一个孩子,所以现在我拥有它我可以设置它的更改。

https://stackoverflow.com/a/8748802/1943671你可以在这个答案中做。

将自定义视图设置为操作栏之后,就像在链接中一样,只需为TextView指定字体:

 Typeface tf = Typeface.createFromAsset(getAssets(),"fonts/yourfont.ttf"); TextView tv = (TextView) findViewById(R.id.customTitle); tv.setTypeface(tf); 

您可以通过创建自定义视图并在ActionBar.膨胀该自定义视图来实现ActionBar.
此自定义视图将包含您将获得引用的TextView ,然后可以设置字体。

请参阅此处的示例: 如何在ActionBar标题中设置自定义字体?

它不受支持。但您可以为操作栏创建自定义视图。 如何在ActionBar标题中设置自定义字体?

 @Override public boolean onCreateOptionsMenu(Menu menu) { //getMenuInflater().inflate(R.menu.activity_main, menu); this.getActionBar().setDisplayShowCustomEnabled(true); this.getActionBar().setDisplayShowTitleEnabled(false); Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/architectsdaughter.ttf"); LayoutInflater inflator = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); View v = inflator.inflate(R.layout.xml_master_footer, null); this.getActionBar().setCustomView(v); ((TextView)v.findViewById(R.id.title)).setText(this.getTitle()); ((TextView) v.findViewById(R.id.title)).setTypeface(tf); //assign the view to the actionbar return true; } 

在你的清单中