在视图寻呼机中使用字体awesome

时间:2015-12-15 19:26:51

标签: android android-viewpager font-awesome pagerslidingtabstrip

我知道如何在View Pager中使用字符串作为标题,但现在我想将字体集成到其中。我不知道如何在View Pager中使用自定义类型的面孔。这是我的viewPagerAdapter代码:

  package com.example.skmishra.customiconsviewpager;

  import android.content.Context;
  import android.graphics.Typeface;
  import android.graphics.drawable.Drawable;
  import android.support.v4.app.Fragment;
  import android.support.v4.app.FragmentManager;
  import android.support.v4.app.FragmentStatePagerAdapter;
  import android.support.v4.app.FragmentTransaction;
  import android.support.v4.view.ViewPager;
  import android.support.v7.app.ActionBar;
  import android.text.Spannable; 
  import android.text.SpannableString;
  import android.text.SpannableStringBuilder;


 public class ViewPagerAdapter extends FragmentStatePagerAdapter implements             ActionBar.TabListener , ViewPager.OnPageChangeListener {

String Titles[]; // This will Store the Titles of the Tabs which are Going to be passed when ViewPagerAdapter is created
int NumbOfTabs; // Store the number of tabs, this will also be passed when the ViewPagerAdapter is created
private final ActionBar mActionBar;
Context context;

         ViewPager mPager;
         Typeface font;

            // Build a Constructor and assign the passed Values to appropriate values in the class
         public ViewPagerAdapter(FragmentManager fm, String mTitles[], int mNumbOfTabsumb, ActionBar mActionBar,Context context,ViewPager pager) {
            super(fm);

            this.Titles = mTitles;
            this.NumbOfTabs = mNumbOfTabsumb;

            this.context=context;

            this.mActionBar = mActionBar;
            this.mPager=pager;
        }

            //This method return the fragment for the every position in the View Pager
        @Override
        public Fragment getItem(int position) {

            if(position==0)
            {
                fragment_tab_bio tab1=new fragment_tab_bio();

                return tab1;
            }
            else if(position==1)
            {
                fragment_tab_movies tab2=new fragment_tab_movies();
                return tab2;
            }
            else
            {
                fragment_tab_funStuff tab3=new fragment_tab_funStuff();
                return tab3;

            }

        }



        @Override
        public CharSequence getPageTitle(int position) {
            Typeface font = Typeface.createFromAsset( context.getAssets(), "fontawesome-webfont.ttf" );
            String title = "";

            title = Titles[position];


            SpannableString styled = new SpannableString(title);
            styled.setSpan(new CustomTypefaceSpan("",font), 0, title.length(), Spannable.SPAN_EXCLUSIVE_INCLUSIVE);

            return styled;
        }
            // This method return the Number of tabs for the tabs Strip

        @Override
        public int getCount() {
            return NumbOfTabs;
        }

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {



        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }

        @Override
        public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {

        }

        @Override
        public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {

        }

        @Override
        public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {

        }
        }

自定义字体跨度类

package com.example.skmishra.customiconsviewpager;

import android.graphics.Paint;
import android.graphics.Typeface;
import android.text.TextPaint;
import android.text.style.TypefaceSpan;

public class CustomTypefaceSpan extends TypefaceSpan {

    private final Typeface newType;

    public CustomTypefaceSpan(String family, Typeface type) {
        super(family);
        newType = type;
    }

    @Override
    public void updateDrawState(TextPaint ds) {
        applyCustomTypeFace(ds, newType);
    }

    @Override
    public void updateMeasureState(TextPaint paint) {
        applyCustomTypeFace(paint, newType);
    }

    private static void applyCustomTypeFace(Paint paint, Typeface tf) {
        int oldStyle;
        Typeface old = paint.getTypeface();
        if (old == null) {
            oldStyle = 0;
        } else {
            oldStyle = old.getStyle();
        }

        int fake = oldStyle & ~tf.getStyle();
        if ((fake & Typeface.BOLD) != 0) {
            paint.setFakeBoldText(true);
        }

        if ((fake & Typeface.ITALIC) != 0) {
            paint.setTextSkewX(-0.25f);
        }

        paint.setTypeface(tf);
    }
 }

1 个答案:

答案 0 :(得分:0)

答案在于Google的slidingTabLayout.java文件。在该文件中只需转到populataTabStrip函数。在该函数中,查找setText(getPageTitle(position)),就在此之前使用setTypeface函数和你的Typeface对象作为参数。你就完成了