TabPageIndicator Text Font

I am using ViewPagerIndicator and I have no luck, so I will ask here. Is there a way to change the font for tabs in TabPageIndicator?

+2
source share
3 answers

You do not need to use another third-party library for this, you can change the TabView class (in TabPageIndicator) (this assumes that you want to have the same font in your application):

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

    }
+5
source

There is no need to use a third-party library or change ViewPagerIndicatorlib.

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

, , 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/

- , .

0

Source: https://habr.com/ru/post/1533904/


All Articles