TabPageIndicator Text Font

前端 未结 3 1692
故里飘歌
故里飘歌 2021-01-07 02:51

I\'m using ViewPagerIndicator, and haven\'t had any luck, so I\'ll ask here. Is there a way to change the font for the Tabs in a TabPageIndicator?

3条回答
  •  时光取名叫无心
    2021-01-07 03:39

    You don't need to use an other third party lib in order to do this, you can modify the TabView class(in TabPageIndicator) like this(this assuming you want the same font in your entire app):

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

提交回复
热议问题