Android custom fonts - for system components like Actionbar, Toast, Dialogs

后端 未结 2 416
萌比男神i
萌比男神i 2021-01-16 03:54

I am working on application translated to languages that are not supported - missing fonts on devices. So I added fonts to application and changed typeface for all TextViews

2条回答
  •  情话喂你
    2021-01-16 04:28

    1. for actionbar:

    use actionbar sherlock (http://actionbarsherlock.com/) and customize its code:

    in com.actionbarsherlock.app.Actionbar add these two abstract methods:

    public abstract void setTitleTypeface(Typeface TF);
    public abstract void setSubtitleTypeface(Typeface TF);
    

    and override these methods in com.actionbarsherlock.internal.app.ActionBarImpl

    @Override
    public void setTitleTypeface(Typeface TF) {
        mActionView.setTitleTypeface(TF);     }
    
    @Override
    public void setSubtitleTypeface(Typeface TF) {
        mActionView.setSubtitleTypeface(TF);    }
    

    and also in com.actionbarsherlock.internal.app.ActionBarWrapper like this

    @Override
    public void setTitleTypeface(Typeface TF) {}
    @Override
    public void setSubtitleTypeface(Typeface TF) {}
    

    and finally in com.actionbarsherlock.internal.widget.ActionBarView add these methods:

     public void setTitleTypeface(Typeface TF){
        mTitleView.setTypeface(TF);
    }
    
    public void setSubtitleTypeface(Typeface TF){
        mSubtitleView.setTypeface(TF);
    }
    

    and now use it in your SherlockActivity like this:

        Typeface TF = Typeface.createFromAsset(getApplication().getAssets(),
                    "Arial.ttf");
        getSupportActionBar().setTitleTypeface(TF);
    

    Make Sure there is no better way!!!

    2. for dialogs, system settings,... u should change code like this!

提交回复
热议问题