2015-11-01 140 views
2

我需要更改我的應用程序工具欄的默認字體,我只是切換到材質設計,它看起來很新,
我正在爲孩子建立一個教育應用程序,所以我需要從專業內置工具欄字體。自定義材質設計工具欄

+1

您可以添加的TextView到工具欄,並設置字體類型給它,工具欄,就像任何佈局可以有孩子的 – k0sh

回答

5

有幾個選項來做到這一點:

創建自定義字體類,並使用SpannableString設置動作條標題是這樣的。

SpannableString s = new SpannableString("My Title"); 
    s.setSpan(new TypefaceSpan(this, "MyTypeface.otf"), 0, s.length(), 
      Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 

    // Update the action bar title with the TypefaceSpan instance 
    ActionBar actionBar = getActionBar(); 
    actionBar.setTitle(s); 

看到這個link得到解決

添加TextViewToolbar內,並設置其屬性

<android.support.v7.widget.Toolbar 
      android:id="@+id/anim_toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" 
      app:layout_collapseMode="pin" 
      app:popupTheme="@style/ThemeOverlay.AppCompat.Light"> 


      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:fontFamily="sans-serif-light" 
       android:text="My custom Text" 
       android:textColor="#fff"/> 

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

試試這個:

SpannableString title = new SpannableString(param); 
    title.setSpan(new TypefaceSpan(context,"Roboto-Regular.ttf"),0,title.length(),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 

//If doing in fragment then 
((AppCompatActivity)getActivity()).getSupportActionBar().setTitle(title) 
//otherwise in activity 
getSupportActionBar().setTitle(title) 

確保您已設置工具欄的操作欄,做到這一點:

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
setSupportActionBar(toolbar); 

如果你沒有工具欄設置爲行動起來吧,然後,一旦你準備Spannable字符串,而不是getSupportActionBar()的setTitle(標題)使用工具欄對象設置標題

toolbar.setTitle(title)