android - TabPageIndicator 文本字体

标签 android viewpagerindicator

我正在使用 ViewPagerIndicator,但没有任何运气,所以我会在这里问一下。有没有办法更改 TabPageIndicator 中选项卡的字体?

最佳答案

您不需要使用其他第三方库来执行此操作,您可以修改 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);

    }

关于android - TabPageIndicator 文本字体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14228186/

相关文章:

android - com.viewpagerindicator.CirclePageIndicator 出错

android - 从 Flowable Room ORM 发出每个项目

java - Android改造向Spring Boot发送put请求时出错?

android - 无法使用 Gradle 将 ViewPagerIndicator 添加到应用程序

Android 滑动屏幕带圆圈指示

java - ViewPagerIndicator - 将 TabPageIndicator 设置到中心

android - 如何在自定义 tablayout 中更改 tablayout 选定的文本颜色

android - onCreateContextMenu 未被调用

android - ActivityResultContracts.TakePicture 返回 "No such file or directory"

java - 我无法将我的 Android 应用程序连接到 MongoDB