问题
I'm using ViewPagerIndicator, and haven't had any luck, so I'll ask here. Is there a way to change the font for the Tabs in a TabPageIndicator?
回答1:
You don't need to use an other third party lib in order to do this, you can modify the TabView class(in TabPageIndicator) like this(this assuming you want the same font in your entire app):
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);
}
回答2:
There is no need to use a third-party lib nor to change ViewPagerIndicator
lib.
TabPageIndicator
has a single ViewGroup
child which contains the tab View
s so you can use this code to change the font:
ViewGroup vg = (ViewGroup) indicator.getChildAt(0);
int vgChildCount = vg.getChildCount();
for (int j = 0; j < vgChildCount; j++) {
View vgChild = vg.getChildAt(j);
if (vgChild instanceof TextView) {
((TextView) vgChild).setTypeface(YOUR_FONT);
}
}
回答3:
So I've kind of brute forced my way into making it work, using a library called Oak (Where the TextViewWithFont is), and passing the PagerIndicator into this method:
private static void changeFonts(ViewGroup root, Activity a, String typeface) {
Typeface tf = TextViewWithFont.getStaticTypeFace(a, typeface);
for (int i = 0; i < root.getChildCount(); i++) {
View v = root.getChildAt(i);
if (v instanceof TextView) {
((TextView) v).setTypeface(tf);
} else if (v instanceof ViewGroup) {
changeFonts((ViewGroup) v, a, typeface);
}
}
}
public static void setTabPageIndicatorFont(ViewGroup root, Activity a) {
changeFonts(root, a, "DINCondBold.otf");
}
Oak if anyones interested: http://willowtreeapps.github.com/OAK/
If anyone has a better way to go about it, I'd love to get a second opinion.
来源:https://stackoverflow.com/questions/14228186/tabpageindicator-text-font