2013-01-09 18 views
2

我使用ViewPagerIndicator,并没有任何运气,所以我会问这里。有没有办法改变TabPageIndicator中Tabs的字体?TabPageIndicator Text Font

+0

这只是另一种方法http://stackoverflow.com/a/17747584/ 542532 – Sadegh

回答

5

你不需要为了做到这一点,以使用其他第三方lib中,你可以修改TabView class(in TabPageIndicator)这样的(这个假设你想在你的整个应用程序相同的字体):

private class TabView extends TextView { 
    private int mIndex; 

    public TabView(Context context) { 
     super(context, null, R.attr.vpiTabPageIndicatorStyle); 
    } 

    @Override 
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
     super.onMeasure(widthMeasureSpec, heightMeasureSpec); 

     // Re-measure if we went beyond our maximum size. 
     if (mMaxTabWidth > 0 && getMeasuredWidth() > mMaxTabWidth) { 
      super.onMeasure(MeasureSpec.makeMeasureSpec(mMaxTabWidth, MeasureSpec.EXACTLY), heightMeasureSpec); 
     } 
    } 

    public int getIndex() { 
     return mIndex; 
    } 

    public TabView(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
     init(); 
    } 

    public TabView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     init(); 
    } 

    public TabView(Context context) { 
     super(context); 
     init(); 
    } 

    private void init() { 

     Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "fonts/myFont.ttf"); // setting a custom TypeFace 
     setTypeface(tf); 

    } 
+0

非常有用!为我工作。 –

0

所以我那种野蛮强迫我的方式为使其工作,使用称为橡木(凡TextViewWithFont是)库,并传递到PagerIndicator这种方法:

private static void changeFonts(ViewGroup root, Activity a, String typeface) { 
    Typeface tf = TextViewWithFont.getStaticTypeFace(a, typeface); 

    for (int i = 0; i < root.getChildCount(); i++) { 
     View v = root.getChildAt(i); 
     if (v instanceof TextView) { 
      ((TextView) v).setTypeface(tf); 
     } else if (v instanceof ViewGroup) { 
      changeFonts((ViewGroup) v, a, typeface); 
     } 
    } 
} 
public static void setTabPageIndicatorFont(ViewGroup root, Activity a) { 
    changeFonts(root, a, "DINCondBold.otf"); 
} 

橡树如果任何人有兴趣:http://willowtreeapps.github.com/OAK/

如果有人有更好的方法去解决这个问题,我希望得到第二个意见。

3

不需要使用第三方库,也不需要更改库文件。

TabPageIndicator有一个单一的ViewGroup孩子包含的标签View这么你可以使用此代码来改变字体:

ViewGroup vg = (ViewGroup) indicator.getChildAt(0); 
int vgChildCount = vg.getChildCount(); 
for (int j = 0; j < vgChildCount; j++) { 
    View vgChild = vg.getChildAt(j); 
    if (vgChild instanceof TextView) { 
     ((TextView) vgChild).setTypeface(YOUR_FONT); 
    } 
}