Androidスタジオ3.0では、単にGoogleフォントをプロジェクト(See android walkthrough)に統合することができます。TextViewにGoogle Fonts .xmlをプログラムで設定する方法
フォントを追加すると、Android StudioはフォントのXMLファイル(私の場合はamatic_sc.xml)を含むフォントフォルダを生成します。また、Studioではvalueフォルダにpreloaded_fonts.xmlを作成します。
amatic_sc.xml:
<?xml version="1.0" encoding="utf-8"?>
<font-family xmlns:app="http://schemas.android.com/apk/res-auto"
app:fontProviderAuthority="com.google.android.gms.fonts"
app:fontProviderPackage="com.google.android.gms"
app:fontProviderQuery="Amatic SC"
app:fontProviderCerts="@array/com_google_android_gms_fonts_certs">
</font-family>
preloaded_fonts.xml:私は私のXMLファイルに静的に次の行が含まれていた場合
<?xml version="1.0" encoding="utf-8"?>
<resources>
<array name="preloaded_fonts" translatable="false">
<item>@font/amatic_sc</item>
</array>
</resources>
は、それが正常に動作します:
android:fontFamily="@font/amatic_sc"
私の場合は、私のカスタムlistAdapterでプログラム的にフォントファミリを設定する必要があります。私は、コードexemples以下試みたが、何も動作:
// Display text with default fontFamily
viewHolder.textView.setTypeface(Typeface.create("@font/amatic_sc", Typeface.NORMAL));
// both throws java.lang.RuntimeException: Font asset not found [../res/]font/amatic_sc.xml
viewHolder.textView.setTypeface(Typeface.createFromAsset(context.getAssets(), "font/amatic_sc.xml"));
viewHolder.textView.setTypeface(Typeface.createFromAsset(context.getAssets(), "../res/font/amatic_sc.xml"));
// both throws java.lang.RuntimeException: Font asset not found [../res/]font/amatic_sc.xml
viewHolder.textView.setTypeface(Typeface.createFromFile("font/amatic_sc.xml"));
viewHolder.textView.setTypeface(Typeface.createFromFile("../res/font/amatic_sc.xml"));
私の場合、私は分SDKのバージョン16を使用して、私は私のコードスニペットは十分あると思います。
ありがとうございました!