2015-12-17 3 views
9

私はアンドロイドカスタムキーボードを開発しました。実際にユニコードを使って出力される出力テキストのフォントスタイルを変更する必要があります。Androidカスタムキーボードの出力フォントを変更することはできますか?

デバイスのデフォルトフォントを変更することなく、キーボードのテキスト出力のフォントスタイルをデバイス全体に変更するにはどうすればよいですか?

フォントはAndroidデバイスにも含まれていないため、キーボードを開発しているのと同じアプリケーションからフォントを外部に秘密にする必要があります。

答えて

4

アプリケーション内のフォントスタイルを変更します。

アプリケーション

という名前のフォントを上書きするために別のクラスを作成
import java.lang.reflect.Field; 
import android.content.Context; 
import android.graphics.Typeface; 

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

FontOverride

をという名前の単純なクラスを作成します今

public final class Application extends android.app.Application { 
    @Override 
    public void onCreate() { 
     super.onCreate(); 
     FontsOverride.setDefaultFont(this, "DEFAULT", "fonts/GeezEdit.ttf"); 
     FontsOverride.setDefaultFont(this, "MONOSPACE", "fonts/GeezEdit.ttf"); 
     /*FontsOverride.setDefaultFont(this, "MONOSPACE", "MyFontAsset2.ttf"); 
     FontsOverride.setDefaultFont(this, "SERIF", "MyFontAsset3.ttf"); 
     FontsOverride.setDefaultFont(this, "SANS_SERIF", "MyFontAsset4.ttf");*/ 
    } 
} 

android:name=".Application" 

これは変更に動作するアプリケーションのタグ内のAndroidマニフェストファイルで最後の言及で、値フォルダ

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

でアンドロイドのスタイルファイルでのスタイルにアプリケーション名をこの書体を追加ユーザーはAndroidプロジェクトまたはアプリケーションにフォントを提供しました。

+1

この回答は機能していますが、アプリケーション外でフォントを変更する必要があります。 –

関連する問題