2012-08-13 28 views
4

在我的應用程序中,我使用ViewPagerPagerTabStrip,我需要爲我的小部件設置自定義字體。要爲ButtonTextView設置字體,我只需擴展該類並設置字體。如何將TypeFace設置爲PagerTBStrip文本視圖

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一起使用的字體?

回答

20

有點晚了,但這裏是一個解決方案。獲取PagerTabStrip的子視圖並檢查它是否是TextView的一個實例。如果是這樣,設置字體:

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) 
    } 
} 
+0

這是醜陋的,但目前爲止可能是最快的解決方案。謝謝=) – ben 2012-12-13 03:13:56

+0

我同意,但我不能想到另一種解決方案,而不是直接抓取源代碼和修改它。沒有支持的API直接更改PagerTabStrip的字體。 – StackOverflower 2013-01-03 21:52:22

1

檢查PagerTabStrip層次結構視圖(在Eclipse中打開運行應用程序時打開層次結構視圖透視圖)並搜索其子項。必須是包含標籤標題的TextView。獲取此TextView並設置其字體。

相關問題