我使用ViewPagerIndicator,并没有任何运气,所以我会问这里。有没有办法改变TabPageIndicator中Tabs的字体?TabPageIndicator Text Font
2
A
回答
5
你不需要为了做到这一点,以使用其他第三方lib中,你可以修改TabView class(in TabPageIndicator)这样的(这个假设你想在你的整个应用程序相同的字体):
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);
}
+0
非常有用!为我工作。 –
0
所以我那种野蛮强迫我的方式为使其工作,使用称为橡木(凡TextViewWithFont是)库,并传递到PagerIndicator这种方法:
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");
}
橡树如果任何人有兴趣:http://willowtreeapps.github.com/OAK/
如果有人有更好的方法去解决这个问题,我希望得到第二个意见。
3
不需要使用第三方库,也不需要更改库文件。
TabPageIndicator
有一个单一的ViewGroup
孩子包含的标签View
这么你可以使用此代码来改变字体:
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);
}
}
相关问题
- 1. Listviews text-font
- 2. font-variant:small-caps; vs text-transform:capitalize;
- 3. Android TabPageIndicator删除小行
- 4. TabPageIndicator上的居中图标
- 5. AsyncTask完成时加载TabPageIndicator
- 6. @ font-face for font variant
- 7. font-weight超过font-face无效?
- 8. 如何使用ViewPager删除TabPageIndicator标签
- 9. 如何使文本中心TabPageIndicator
- 10. ViewPagerIndicator - 将TabPageIndicator设置为中心
- 11. TabPageIndicator中制表符的宽度相同
- 12. ListView with Viewpager作为TabPage里面的TabPageIndicator
- 13. 选项卡之间的间距TabPageIndicator
- 14. ActionBarSherlock TabPageIndicator with ViewPager.OnPageChangeListener - 选项卡卡住
- 15. Eclipse Android Text Text Text = String text
- 16. 为什么bill.xpath(“// p/font/a”)[index] .text使用Nokogiri返回“nil:NilClass”的未定义方法“text”?
- 17. @ font-face问题
- 18. @ font-face与cufon
- 19. 为什么不在@ font-face,Font Squirrel中定义font-weight或font-style?
- 20. 变化FONT-FAMILY
- 21. Font Resizer if else
- 22. 使用@ font-face
- 23. 网页字体:@ font-face应该包含font-style和font-weight?
- 24. 当使用sf :: Text和sf :: Font时,无法解析的外部符号
- 25. @ font-face问题
- 26. setAttribute和text
- 27. font-size/font-family没有效果
- 28. “font-family”和font-family之间的区别?
- 29. @ font-face和Raphael VML textpath font-family
- 30. 导入新字体@ font-face font-family
这只是另一种方法http://stackoverflow.com/a/17747584/ 542532 – Sadegh