自定义材料设计工具栏

时间:2015-11-01 07:22:35

标签: android material-design

我需要更改应用程序工具栏的默认字体,我只是切换到材料设计,它看起来很新,
我正在为孩子们建立一个教育应用程序,所以我需要从专业的内置工具栏字体更改。

2 个答案:

答案 0 :(得分:5)

有几种方法可以做到这一点:

创建一个自定义TypeFace类,并使用SpannableString像这样设置ActionBar标题。

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以获取解决方案

TextView中添加Toolbar并设置其属性

    <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>

答案 1 :(得分: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 String而不是getSupportActionBar()。setTitle(title)使用工具栏对象来设置标题

toolbar.setTitle(title)
相关问题