2016-04-02 4 views
2

私の全体プロジェクトのフォントを変更するには、chrisjenx/Calligraphy in Themeを使います。Android chrisjenx/Calligraphy Themeで定義されたカスタムフォントTheme.AppCompatが動作しない

私は指示に従ったが、それはここに私のため

を働いていない私のコードです:アプリケーション内の

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



@Override 
public void onCreate() { 
    super.onCreate(); 
    CalligraphyConfig.initDefault(new CalligraphyConfig.Builder() 
          .setDefaultFontPath("BElham.ttf") 
          .setFontAttrId(R.attr.fontPath) 
          .build() 
      ); 
} 

ここに私のスタイルは、最初に作成し、これを試してください

<!-- Base application theme. --> 
<style name="AppBaseTheme" parent="@style/Theme.AppCompat.Light.NoActionBar">> 
    <item name="android:textViewStyle">@style/AppTheme.Widget.TextView</item> 
</style> 

<style name="AppTheme" parent="AppBaseTheme"> 
    <item name="colorPrimary">@color/colorPrimary</item> 
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item> 
    <item name="colorAccent">@color/colorAccent</item> 
</style> 

<style name="AppTheme.Widget"/> 

<style name="AppTheme.Widget.TextView" parent="android:Widget.TextView"> 
    <item name="fontPath">BElham.ttf</item> 
</style> 
+0

http://mobikul.com/android-calligraphy/ – Killer

答えて

2

のようなフォントを定義してはいけませんアプリケーションクラス内の。 ベターBaseActivityでこれを入れて、私は最後にあなたのコードを使用して実装することができる午前、他の全ての活動に

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

それを拡張します。

1

ですアプリケーションクラス

MyApplication.java

@Override 
public void onCreate() { 
TypefaceUtil.overrideFont(getApplicationContext(), "SERIF", "fonts/Roboto-Regular.ttf"); // font from assets: "assets/fonts/chrisjenx.ttf 
} 

そして、例えばのためにそれ

ザッツ

<style name="MyMaterialTheme.Base"parent="Theme.AppCompat.Light.DarkActionBar"> 

    <item name="android:typeface">serif</item> 

</style> 

を追加するには、テーマ内でごstyle.xmlで

import android.content.Context; 
import android.graphics.Typeface; 
import java.lang.reflect.Field; 

public class TypefaceUtil { 


public static void overrideFont(Context context, 
     String defaultFontNameToOverride, String customFontFileNameInAssets) { 
    try { 
     final Typeface customFontTypeface = Typeface.createFromAsset(
       context.getAssets(), customFontFileNameInAssets); 

     final Field defaultFontTypefaceField = Typeface.class 
       .getDeclaredField(defaultFontNameToOverride); 
     defaultFontTypefaceField.setAccessible(true); 
     defaultFontTypefaceField.set(null, customFontTypeface); 
    } catch (Exception e) { 
     System.out.println("Can not set custom font " 
       + customFontFileNameInAssets + " instead of " 
       + defaultFontNameToOverride); 
    } 
    } 
} 

TypefaceUtil.java

クラス を作成
0

あなたはcreatする必要がありますEAフォルダがアセットフォルダ内の「フォント」と命名し、それらのフォントを入れ フォント・パスは、このassets/fonts/BElham.ttf ようになり、その後、各アクティビティにコードを次の書き込みにあなたが持っている

@Override 
public void onCreate() { 
super.onCreate(); 
CalligraphyConfig.initDefault(new CalligraphyConfig.Builder() 
         .setDefaultFontPath("fonts/BElham.ttf") 
         .setFontAttrId(R.attr.fontPath) 
         .build() 
     ); 
} 
+0

私はまだしていませんでした – user987760

+0

あなたのフォントパスを変更しましたか? '.setDefaultFontPath(" fonts/BElham.ttf ")' – Max

関連する問題