2017-06-05 93 views
4

有沒有什麼辦法在android棒棒糖中使用geomanist(自定義)字體。自定義字體不工作在棒棒糖

但它適用於所有其他版本。

這裏是我的代碼

MyApplication的類

FontsOverride.setDefaultFont(this, "DEFAULT", "geomanist-lightnew.ttf"); 
    FontsOverride.setDefaultFont(this, "MONOSPACE", "geomanist-lightnew.ttf"); 
    FontsOverride.setDefaultFont(this, "SERIF", "geomanist-lightnew.ttf"); 
    FontsOverride.setDefaultFont(this, "SANS_SERIF", "geomanist-lightnew.ttf"); 

public final class FontsOverride { 

public static void setDefaultFont(Context context, 
            String staticTypefaceFieldName, String fontAssetName) { 
    final Typeface regular = Typeface.createFromAsset(context.getAssets(), 
      fontAssetName); 
    replaceFont(staticTypefaceFieldName, regular); 
} 

protected static void replaceFont(String staticTypefaceFieldName, 
            final Typeface newTypeface) { 
    try { 
     final Field staticField = Typeface.class 
       .getDeclaredField(staticTypefaceFieldName); 
     staticField.setAccessible(true); 
     staticField.set(null, newTypeface); 
    } catch (NoSuchFieldException e) { 
     e.printStackTrace(); 
    } catch (IllegalAccessException e) { 
     e.printStackTrace(); 
    } 



} 




} 

感謝

+0

讀https://stackoverflow.com/questions/33923803/how-to-set-custom-字體爲整個應用程序在Android –

回答

3

我用這個Library和它的作品精湛的所有設備上的

步驟: -

1)添加此行中的gradle這個compile 'com.github.balrampandey19:FontOnText:0.0.1'

2)在資源文件夾添加字型這樣

3)然後替換XML格式的視圖與

<com.balram.library.FotTextView 
        android:id="@+id/vno_tv" 
        . 
        . 
        android:textSize="14sp" 
        app:font="regular.ttf" /> 

此行對設置自定義字體很重要app:font="regular.ttf"

可以爲ButtonsEdittext

OR

如果您想使用相同的 「字體」,通過整個應用程序,你可以按照這個 Guide here

+0

我需要改變整個應用程序的geomanist字體。不適用於特定的文本視圖。無論如何感謝您的回覆。 @Akshay Katariya –

+0

我認爲這可能會更有幫助,然後https://stackoverflow.com/a/42218846/4762767 –

+0

(stackoverflow.com/a/42218846/4762767)非常有用的鏈接。它的工作正常。謝謝@ AkshayKatariya –