2013-07-04 67 views
3

在我的項目中,我使用字體android:fontFamily =「sans-serif-light」,並且正常工作。在viewpagerindicator中使用自定義字體

我也在使用library viewpagerindicator。我想在viewpagerindicator中使用字體android:fontFamily =「sans-serif-light」,但無法找到如何去做

我試過在<com.viewpagerindicator.TitlePageIndicator ...中使用android:fontFamily = "sans-serif-light",但是沒有成功。

我也曾嘗試:

PageIndicator mIndicator = (TitlePageIndicator) findViewById (R.id.indicator); 
Typeface myTypeface = Typeface.createFromAsset (getAssets(), "fonts/Roboto-Light.ttf"); 
mIndicator.setTypeface (myTypeface); 

但這並不工作..

我感謝所有幫助。

感謝和問候

+0

看到我在這裏發佈的解決方案:http:// stackoverflow。com/a/21786373/752781 – pandre

回答

10

如果我沒有得到你錯了,你想改變標題字體鑑於尋呼機指標,

我改變了圖書館實現這一目標, 爲TabPageIndicator custome字體我加這對於TabPageIndicator.java

private Typeface      mTypeface; 
public void setTypeFace(Typeface tf) { 
    this.mTypeface = tf; 
    } 

,然後更改addTab功能如下:

private void addTab(int index, CharSequence text, int iconResId) { 
final TabView tabView = new TabView(getContext()); 
tabView.mIndex = index; 
tabView.setFocusable(true); 
tabView.setOnClickListener(mTabClickListener); 
tabView.setText(text); 


**if (mTypeface != null) { 
    tabView.setTypeface(mTypeface); 
}** 


if (iconResId != 0) { 
    tabView.setCompoundDrawablesWithIntrinsicBounds(iconResId, 0, 0, 0); 
} 

mTabLayout.addView(tabView, new LinearLayout.LayoutParams(0, MATCH_PARENT, 1)); 
} 

現在你應該只setTypeFace您tabpagerindicator,像這樣:

mTabPageIndicator = (TabPageIndicator) findViewById(R.id.tab_page_indicator); 
mTabPageIndicator.setTypeFace(Typeface.createFromAsset(getApplicationContext().getAssets(), "fonts/custome_font.ttf"); 
0

我使用ViewPageIndicator庫,這裏是我做到了(指數是在視圖頁面指示符選項卡索引):

private void changeTabText(int index, boolean on){ 
    if(tabLayout.getChildCount()-1 >= index){ 
     TextView tv = (TextView)tabLayout.getChildAt(index); 
     tv.setTextColor(getResources().getColor(on? android.R.color.black : R.color.light_grey)); 
     CustomFont.setTypeface(tv, CustomFont.NORMAL); 
    } 
} 

這裏就是我得到的tablayout:

tabLayout = (ViewGroup)indicator.getChildAt(0); //indicator is a horizontal scroll view, there will be only one root layout 

這裏是我的自定義字體做什麼:

public static void setTypeface(TextView view, String font) { 
    Typeface typeface = Typeface.createFromAsset(view.getContext().getAssets(), BASE_PATH + font); 
    view.setTypeface(typeface); 
} 
0

更OOD的實現是通過創建一個新的界面修改庫:

public interface FontPagerAdapter { 
/** 
* Get the fonts to set. 
*/ 
Typeface getCustomFont();} 

,並在類TabPageIndicator.java添加新的屬性:

private Typeface customTypeFace; 

這將是通過聲明設置在notifyDataSetChanged()方法中:

if (adapter instanceof FontPagerAdapter) { 
     FontPagerAdapter fontAdapter = (FontPagerAdapter)adapter; 
     customTypeFace = fontAdapter.getCustomFont(); 
    } 

以後你會改變字體由addTab方法編程設置,只是通過增加:最後

if (customTypeFace != null) { 
    tabView.setTypeface(customTypeFace); 
} 

中,將使用該庫,您需要實現此接口的適配器,然後覆蓋的方法:

@Override 
public Typeface getCustomFont() { 
    Typeface font = Typeface.createFromAsset(context.getAssets(),"fonts/PoetsenOne-Regular.ttf"); 
    return font; 
}