如何更改pagertitlestrip的字体

Dri*_*sha 4 android android-viewpager

Typeface xyz=Typeface.createFromAsset(view.getContext().getAssets(),
                "fonts/xyz.ttf");
(TextView)abc.setTypeface(xyz);
Run Code Online (Sandbox Code Playgroud)

这是您创建和设置自定义字体的方式.

我需要设置一个字体/字体,pagertitlestrip但没有.setTypeFace功能pagertitlestrip.有谁知道我怎么能这样做?

jbu*_*s20 9

PagerTitleStrip实际上是扩展ViewGroup,所以你可以继承它,并遍历它的每个孩子找到那些需要它们的字体要改变的观点:

public class CustomFontPagerTitleStrip extends PagerTitleStrip {
    public CustomFontPagerTitleStrip(Context context) {
        super(context);
    }
    public CustomFontPagerTitleStrip(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "fonts/xyz.ttf");
        for (int i=0; i<this.getChildCount(); i++) {
            if (this.getChildAt(i) instanceof TextView) {
                ((TextView)this.getChildAt(i)).setTypeface(tf);
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

唯一的缺点是它阻止Eclipse能够显示您的布局.