0
我知道如何在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);
}
}
那麼這段代碼發生了什麼?結果如何與你想要/期望的結果不同?順便說一句,不要爲每個標題加載字體真棒。只需加載一次(到你已經聲明但從未使用的'font'字段中)。 –
我似乎無法將類型面設置爲spannable –
您使用的是哪個CustomTypefaceSpan類? –