2015-11-01 139 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)