2013-07-31 31 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是工具欄的第一個孩子,所以現在我有它,我可以設置更改。

相關問題