2017-12-07 4 views
0

私の以前のプロジェクトでは、Calligraphyライブラリを使ってアプリケーション全体のフォントを設定しました。しかし、APKサイズを大きくするためにフォントファイルを資産に格納する必要があります。これで、アプリケーション全体のデフォルトとしてdownloadable fontを設定することができるのだろうかと思います。アプリケーション全体のダウンロード可能なフォントをデフォルトのフォントとして設定するにはどうすればよいですか?

1つのTextViewにのみダウンロード可能なフォントを設定できました。

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_margin="@dimen/text_margin" 
    android:fontFamily="@font/lato" 
    android:text="@string/large_text" /> 

はい、私はMyTextViewクラスを作成し、プログラムのダウンロードフォントを設定することができます知っています。しかし、テキストはEditText、Spinner項目、Toastのどこにでも置くことができるので、良いアイデアだとは思わない。

私の質問は、アプリケーション全体のダウンロード可能なフォントをデフォルトのフォントとして設定する方法です。

答えて

0

A library to help with custom fonts and text sizes

このライブラリの目標は、あなたのアプリが簡単に設定可能な方法で独自のスタイル(例えば、通常の太字、斜体)を持つ複数のFontFamilies(例えばラト、roboto、など)をサポートできるようにすることです。

+0

しかし、このライブラリはアプリケーション全体では機能しません –

0

アプリ内のどこにでもXMLのフォントセットを適用するには、themes.xmlにテーマを作成し、その中にandroid:fontFamilyを設定します。マニフェストでアプリケーションに

<application 
    android:name=".App" 
    android:icon="@mipmap/ic_launcher" 
    android:theme="@style/ApplicationTheme"> 
... 
</application> 

と限り、あなたはあなたのフォントが適用されます

<style name="CustomButton" parent="Base.Widget.AppCompat.Button.Borderless"> 

のようなシステムのスタイルを継承したスタイルを使用していないように、このテーマを設定し

<style name="ApplicationTheme"> 
    <item name="android:fontFamily">@font/lato</item> 
</style> 

すべてのテキストビュー、編集テキスト、スピナー、トーストなど

0
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(); 
     } 
    } 
} 


public final class Application extends android.app.Application { 
    @Override 
    public void onCreate() { 
     super.onCreate(); 
     FontsOverride.setDefaultFont(this, "DEFAULT", "MyFontAsset.ttf"); 
     FontsOverride.setDefaultFont(this, "MONOSPACE", "MyFontAsset2.ttf"); 
     FontsOverride.setDefaultFont(this, "SERIF", "MyFontAsset3.ttf"); 
     FontsOverride.setDefaultFont(this, "SANS_SERIF", "MyFontAsset4.ttf"); 
    } 
} 
関連する問題