Using custom font in viewpagerindicator

我只是一个虾纸丫 提交于 2019-12-06 00:59:08

If I don't get you wrong, you want to change titles font in view pager indicator,

I changed the library to achieve that, for TabPageIndicator custome typeface I added this for TabPageIndicator.java

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

and then change addTab function to this:

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

now you should just setTypeFace on your tabpagerindicator, like this:

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

I'm using the ViewPageIndicator library, here's how I did it (index is the tab index in your view page indicator):

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

Here's how I got the tablayout:

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

Here's what my custom font does:

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

A more OOD implementation is to modify the Library by creating a new interface:

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

And in class TabPageIndicator.java add a new property:

private Typeface customTypeFace;

which will be set in the notifyDataSetChanged() method by declaring:

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

Later you would change the Font by setting it programatically in the addTab method, just by adding:

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

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

@Override
public Typeface getCustomFont() {
    Typeface font = Typeface.createFromAsset(context.getAssets(),"fonts/PoetsenOne-Regular.ttf");
    return font;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!