問題描述
我開發了一個 android 自定義鍵盤.我需要更改實際使用 unicode 打印的輸出文本的字體樣式.
I have developed an android custom keyboard. I need to change the font style of my output text which are actually printed using the unicodes.
如何在不更改設備默認字體的情況下更改設備中任何位置的鍵盤文本輸出的字體樣式?
How can I change the font style of my keyboard's text output anywhere throughout the device without having to change the device's default font?
字體也不在安卓設備中,所以我們必須從開發鍵盤的同一個應用程序外部提供字體.
The font is also not in android device, so we have to privide the font externally from the same application which is developing the keyboard.
推薦答案
更改應用程序內的字體樣式.
changing the font style inside the application.
創建一個名為
字體覆蓋
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();
}
}
}
現在創建另一個類來覆蓋名為
now create another class to override the fonts named
應用
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 樣式文件中的樣式
now add this typeface to style in android style file at values folder
<item name="android:typeface">monospace</item>
最后在應用標簽內的android Manifest文件中提到應用名稱
At last mention the Application name at android Manifest file inside application tag
android:name=".Application"
這將有助于將用戶提供的字體更改為 android 項目或應用程序.
This will work to change the user provided font to the android project or application.
這篇關于我可以更改 Android 自定義鍵盤的輸出字體嗎?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!