2015-12-02 44 views
1

是否可以在Android應用程序中設置自定義字體?爲什麼我的自定義字體未應用於所有應用程序?

我試了一下張貼here,但我不知道我的extends Application類...

任何幫助嗎?

編輯:

我試過如下:

  • 添加資產文件夾,然後插入字體內側,在這裏看到:

enter image description here

  • 添加從Application延伸的新班級

  • 從我的AndroidManifest.xml調用此新班級。

  • 我去了我的風格,並添加它。

MyApp.java:

public class MyApp extends Application { 
    @Override 
    public void onCreate() { 
    super.onCreate(); 
    FontsOverride.setDefaultFont(this, "DEFAULT", "raleway_regular.ttf"); 
    // This FontsOverride comes from the example I posted above 
    } 
    } 

的AndroidManifest.xml:

<application 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:supportsRtl="true" 
     android:name=".MyApp" 
     android:theme="@style/AppTheme"> 
    .... 

styles.xml:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> 
    <item name="android:fontFamily">default</item> 
</style> 

但我的字體仍然沒有改變......任何想法?

然後調用MyApp類。但對我的字體沒有影響...

編輯2:我意識到我的按鈕在爲我的按鈕設置自定義樣式後應用自定義字體。這裏是我的自定義按鈕樣式:

<style name="MyButtonStyle" parent="Widget.AppCompat.Button"> 
    <item name="textAllCaps">false</item> 
    <item name="android:textAllCaps">false</item> 
</style> 

這裏是現在的樣子:

enter image description here

所以:我的按鈕應用樣式,而不是TextView。任何想法爲什麼我的自定義字體不適用於應用程序中的所有項目?

回答

1

試試看看這個代碼。但是你需要在findViewByIdMethod(int id)的代碼中初始化你的TextView或Button。

TextView yourTextView = findViewById(R.id.YourTextViewId);  
Typeface font = Typeface.createFromAsset(getActivity().getAssets(),"raleway_regular.ttf"); 
yourTextView.setTypeface(font); 
+0

我知道有一種方法可以一個接一個......並且這種方法可以一個一個正常工作。但我想通常... – Sonhja

1

存在於Android的自定義字體篦庫:custom fonts

這裏是一個示例如何使用它。

在gradle這個

你需要把此行

compile 'uk.co.chrisjenx:calligraphy:2.1.0' 

然後進行擴展應用的寫這個代碼的類`公共類應用程序擴展應用{ @覆蓋 公共無效的onCreate(){ 超.onCreate();

CalligraphyConfig.initDefault(new CalligraphyConfig.Builder() 
        .setDefaultFontPath("your font path") 
        .setFontAttrId(R.attr.fontPath) 
        .build() 
    ); 
} 

}`

,並把這種方法的onCreate前的活動課。

@Override 
protected void attachBaseContext(Context newBase) { 
    super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase)); 

和清單文件中的最後一件事情就是這樣寫的。

它會改變整個活動到你的字體!它簡單而乾淨!

+0

我不是在尋找一個安卓字體...... Acutally我必須使用「Raleway」字體...... – Sonhja

+0

它不是一種字體來改變你的字體,你可以添加任何你想要的字體,它會出現每一個地方如果你想我可以發佈我的代碼如何使用它 –

+0

哦,這是一個自定義庫! :哦,我必須試一試! – Sonhja

相關問題