How to change ActionBar title font when using AppCompat

后端 未结 3 840
时光取名叫无心
时光取名叫无心 2021-02-02 00:06

I would like to apply custom font to the title of my app which is displayed on the ActionBar. Previously I didn\'t use any support library and this solution:

int         


        
3条回答
  •  孤城傲影
    2021-02-02 00:16

    Use Toolbar instead of ActionBar.follow below steps to add toolbar: 1.change your app theme to Theme.AppCompat.Light.NoActionBar 2.Goto project structure from File menu,goto library dependencies,add design library and sync your project. 3.Goto your layout where you want to display ActionBar and write below code:

    
      
    
    

    4.In the Activity,write as below:

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

    To customize your title font:

    fontPath = "Fonts/Roboto-Light.ttf";
        Typeface tf = Typeface.createFromAsset(getActivity().getAssets(), fontPath);
    
    TextView tv_ins=(TextView)FindViewById(R.id.toolbar_title);
     tv_ins.setText("mytitle");
    tv_ins.setTypeface(tf);
    

    Try this out Hopes this helps you,if anything wrong please let me know.

提交回复
热议问题