Using custom font in viewpagerindicator

In my project, I use the font android: fontFamily = "sans-serif-light" and it works fine.

I also use the viewpagerindicator library. I want to use the font android: fontFamily = "sans-serif-light" also in viewpagerindicator, but I can not find how to do it

I tried using android:fontFamily = "sans-serif-light"in <com.viewpagerindicator.TitlePageIndicator ...and in style, but to no avail.

I also tried:

PageIndicator mIndicator = (TitlePageIndicator) findViewById (R.id.indicator);
Typeface myTypeface = Typeface.createFromAsset (getAssets (), "fonts / Roboto-Light.ttf");
mIndicator.setTypeface (myTypeface);

but it does not work.

I appreciate any help.

Thank you and welcome

+3
source share
3 answers

If I'm not mistaken, you want to change the font of the headers as a pager indicator,

, , TabPageIndicator custome typeface TabPageIndicator.java

private Typeface                       mTypeface;
public void setTypeFace(Typeface tf) {
    this.mTypeface = tf;
    }

addTab :

    private void addTab(int index, CharSequence text, int iconResId) {
final TabView tabView = new TabView(getContext());
tabView.mIndex = index;
tabView.setFocusable(true);
tabView.setOnClickListener(mTabClickListener);
tabView.setText(text);


**if (mTypeface != null) {
    tabView.setTypeface(mTypeface);
}**


if (iconResId != 0) {
    tabView.setCompoundDrawablesWithIntrinsicBounds(iconResId, 0, 0, 0);
}

mTabLayout.addView(tabView, new LinearLayout.LayoutParams(0, MATCH_PARENT, 1));
}

TypeFace -, :

mTabPageIndicator = (TabPageIndicator) findViewById(R.id.tab_page_indicator);
mTabPageIndicator.setTypeFace(Typeface.createFromAsset(getApplicationContext().getAssets(), "fonts/custome_font.ttf");
+10

ViewPageIndicator, , ( - ):

private void changeTabText(int index, boolean on){
    if(tabLayout.getChildCount()-1 >= index){
        TextView tv = (TextView)tabLayout.getChildAt(index);
        tv.setTextColor(getResources().getColor(on? android.R.color.black : R.color.light_grey));
        CustomFont.setTypeface(tv, CustomFont.NORMAL);
    }
}

tablayout:

tabLayout = (ViewGroup)indicator.getChildAt(0); //indicator is a horizontal scroll view, there will be only one root layout

:

 public static void setTypeface(TextView view, String font) {
    Typeface typeface = Typeface.createFromAsset(view.getContext().getAssets(), BASE_PATH + font);
    view.setTypeface(typeface);
}
0

OOD - :

public interface FontPagerAdapter {
/**
 * Get the fonts to set.
 */
Typeface getCustomFont();}

TabPageIndicator.java :

private Typeface customTypeFace;

notifyDataSetChanged(), :

if (adapter instanceof FontPagerAdapter) {
        FontPagerAdapter fontAdapter = (FontPagerAdapter)adapter;
        customTypeFace = fontAdapter.getCustomFont();
    }

Font, addTab, :

if (customTypeFace != null) {
   tabView.setTypeface(customTypeFace);
}

Finally, in the adapter that will use the library, you need to implement this interface, and then override the method:

@Override
public Typeface getCustomFont() {
    Typeface font = Typeface.createFromAsset(context.getAssets(),"fonts/PoetsenOne-Regular.ttf");
    return font;
}
0
source

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


All Articles