如何设置字体为PagerTabStrip文本视图视图、如何设置、文本、字体

由网友(晿一半、旳歌 | 聽一半)分享简介:在我的应用程序,我使用的是 ViewPager 有 PagerTabStrip ,然后我需要设置自定义字体我的小部件。要设置字体为按钮或的TextView 我只是扩展了类,并设置字体。 In my app I'm using a ViewPager with a PagerTabStrip and I need to...

在我的应用程序,我使用的是 ViewPager 有 PagerTabStrip ,然后我需要设置自定义字体我的小部件。要设置字体为按钮的TextView 我只是扩展了类,并设置字体。

In my app I'm using a ViewPager with a PagerTabStrip and I need to set custom fonts to my widgets. To set a font for a Button or a TextView I just extend the class and set a typeface.

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

But I can't use this method for a PagerTabStrip. Is there another way to set a typeface that can be used with PagerTabStrip?

推荐答案

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

A little late but here is one solution. Get the child views of the PagerTabStrip and check if it is an instance of a TextView. If it is, set the typeface:

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)
    }
}
阅读全文

相关推荐

最新文章