TabPageIndicator文本字体

时间:2013-01-09 04:16:01

标签: android viewpagerindicator

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

3 个答案:

答案 0 :(得分: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);

    }

答案 1 :(得分:3)

无需使用第三方库,也无需更改ViewPagerIndicator lib。

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);
    }
}

答案 2 :(得分:0)

所以我有点粗暴强迫我的方法使它工作,使用一个名为Oak的文件夹(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/

如果有人有更好的方法去做,我很想获得第二意见。

相关问题