2013-07-31 59 views
4

我有一个字体,我想更改android中的动作栏标题的字体。 有没有一种方法来设置这样的标题字体?更改android中的应用程序标题的字体

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

这不是一个副本问题,这个链接(How to Set a Custom Font in the ActionBar Title?)这些方法都不能正常工作。 当我尝试它们,日食给出了错误:java.lang.noSuchMethodError

+0

你的意思是在'ActionBar'中改变应用程序的标题吗? –

+0

是的,我正在编辑这个问题。 – anilbey

回答

0

你可以通过创建一个自定义视图和膨胀的ActionBar.
该自定义视图这个自定义视图将包含一个TextView你会得到一个参考,然后能够设置一个字体。

在这里看到一个例子:How to Set a Custom Font in the ActionBar Title?

+0

我已经在发布我的问题之前尝试过了。那些页面上的解决方案不起作用:( – anilbey

+0

更好的是发布你的代码,也许有人可以找出它为什么不起作用 –

+0

但是,我的代码与该主题无关,我想要做的就是, 'setTitleTypeface(myTypeface);' – anilbey

1

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); 
+1

如何找到我的动作栏标题的ID?显然,它不是customTitle。 – anilbey

+2

以及那个页面中的答案:“http://stackoverflow.com/a/8748802/1943671 “不起作用。 – anilbey

0

这不是supported.But您可以为您的操作栏自定义视图。 How to Set a Custom Font in the ActionBar Title?

@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; 
} 

,并在您menifest

<uses-sdk 
    android:minSdkVersion="11" 
    android:targetSdkVersion="16" /> 
+1

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

+0

它需要最低的API版本11.请检查你的 –

+0

我的API版本是15 – anilbey

14

试试这个,

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); 
} 

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

<android.support.v7.widget.Toolbar 
android:id="@+id/toolbar" 
android:layout_height="wrap_content" 
android:layout_width="match_parent" 
android:minHeight="?attr/actionBarSize" 
android:background="@color/action_color” 
app:theme="@style/ToolBarTheme" > 


<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Title" 
    android:layout_gravity="center" 
    android:id="@+id/title_txt” /> 


</android.support.v7.widget.Toolbar> 

然后只需通过编程方式使用下面的评论添加任何样式。使用样式

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
TextView mTitle = (TextView) toolbar.findViewById(R.id.title); 

OR

变化。需要一个信息click here

+0

好的解决方案。一旦你有了TextView,你可以用任何你喜欢的方式来设计它。 – PeteH

+0

工作完美,如此简单。希望未来能够得到证明? – KickingLettuce

+0

谢谢!很好的解决方案。 – Alex

2

我发现this answer headsvk帮助我。做一些调整,我只需要做到这一点:

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

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

相关问题