如何将TypeFace设置为PagerTabStrip文本视图

4

我的应用程序中使用了一个带有ViewPagerPagerTabStrip的控件,我需要为它们设置自定义字体。为了为ButtonTextView设置字体,我只需扩展该类并设置字体类型。

public class MyButton extends Button {

    public MyButton(Context context) {
        super(context);
        initCustomFont();
    }

    public MyButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        initCustomFont();
    }

    public MyButton(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initCustomFont();
    }

    private void initCustomFont() {
        if(!isInEditMode()) {
            Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "fonts/boton_regular.ttf");
            setTypeface(tf);
        }
    }
}

但是我不能用这种方法来设置PagerTabStrip的字体。是否有其他方法可以设置与PagerTabStrip兼容的字体?

2个回答

20

有点晚了,但这里有一个解决方案。获取PagerTabStrip的子视图并检查是否为TextView的实例。如果是,则设置字体:

for (int i = 0; i < pagerTabStrip.getChildCount(); ++i) {
    View nextChild = pagerTabStrip.getChildAt(i);
    if (nextChild instanceof TextView) {
       TextView textViewToConvert = (TextView) nextChild;
       textViewToConvert.setTypeface(PUT_TYPEFACE_HERE)
    }
}

这很丑,但目前可能是最快的解决方案。谢谢 =) - ben
我同意,但除了直接获取源代码并进行修改之外,我想不到其他解决方案。没有支持的API可以直接更改PagerTabStrip的字体。 - StackOverflower

1

使用Hierarchy View检查PagerTabStrip(在应用程序运行时,在Eclipse中打开Hierarchy View视图),并搜索其子项。其中一个必须是包含选项卡标题的TextView。获取此TextView并设置其字体。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接