Custom font for ActionBarSherlock tabs

后端 未结 6 2021
滥情空心
滥情空心 2020-12-16 02:13

I want to set font for the \"Video\" and \"Image\" tabs in ActionBarSherlock. I have used the following code to do so. Its showing accurately in ICS but not in

6条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-16 02:30

    First create following custom TypefaceSpan class in your project.Bit changed version of Custom TypefaceSpan to enable to use both .otf and .ttf fonts.

    import java.util.StringTokenizer;
    
    import android.content.Context;
    import android.graphics.Typeface;
    import android.support.v4.util.LruCache;
    import android.text.TextPaint;
    import android.text.style.MetricAffectingSpan;
    
    public class TypefaceSpan extends MetricAffectingSpan{
    
        /*Cache to save loaded fonts*/
        private static LruCache typeFaceCache= new LruCache(12);
        private Typeface mTypeface;
        public TypefaceSpan(Context context,String typeFaceName)
        {
            StringTokenizer tokens=new StringTokenizer(typeFaceName,".");
            String fontName=tokens.nextToken();
            mTypeface=typeFaceCache.get(fontName);
    
            if(mTypeface==null)
            {
                mTypeface=Constants.getFont(context, typeFaceName);
                //cache the loaded font
                typeFaceCache.put(fontName, mTypeface);
            }
        }
        @Override
        public void updateMeasureState(TextPaint p) {
            p.setTypeface(mTypeface);
        }
    
        @Override
        public void updateDrawState(TextPaint tp) {
            tp.setTypeface(mTypeface);
        }
    
    }
    

    Now apply code like this:(I used this on one of my Bangla apps successfully)

            SpannableString mstKnwTitle=new SpannableString(getString(R.string.e_mustknow_tab));
            SpannableString cntctsTitle=new SpannableString(getString(R.string.e_number_tab));
    
    
            TypefaceSpan span=new TypefaceSpan(this, "solaimanlipi.ttf");
    
            mstKnwTitle.setSpan(span, 0, mstKnwTitle.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);     
            cntctsTitle.setSpan(span, 0, mstKnwTitle.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    
            Tab tab= actionBar.newTab();
            tab.setText(mstKnwTitle);
            tab.setTabListener(tabListener);
    
            actionBar.addTab(tab);
    
    
            tab= actionBar.newTab();
            tab.setText(cntctsTitle);
            tab.setTabListener(tabListener);
    
            actionBar.addTab(tab);
    

    Original inspiration of my answer was:Styling the Android Action Bar title using a custom typeface

提交回复
热议问题