如何设置字体为PagerTabStrip文本视图 [英] How to set a TypeFace to PagerTabStrip text views
本文介绍了如何设置字体为PagerTabStrip文本视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的应用程序,我使用的是 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)
}
}
这篇关于如何设置字体为PagerTabStrip文本视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文