Ank*_*ist 9 keyboard fonts android
我开发了一个Android自定义键盘.我需要更改使用unicodes实际打印的输出文本的字体样式.
如何在整个设备的任何位置更改键盘文本输出的字体样式,而无需更改设备的默认字体?
该字体也不在Android设备中,因此我们必须从开发键盘的同一应用程序外部提供字体.
小智 5
更改应用程序内的字体样式。
创建一个名为的简单类
字体覆盖
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();
}
}
}
Run Code Online (Sandbox Code Playgroud)
现在创建另一个类来覆盖命名的字体
应用
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");*/
}
}
Run Code Online (Sandbox Code Playgroud)
现在将此字体添加到值文件夹中的 android 样式文件中的样式
<item name="android:typeface">monospace</item>
Run Code Online (Sandbox Code Playgroud)
最后在应用程序标签内的android Manifest文件中提到应用程序名称
android:name=".Application"
Run Code Online (Sandbox Code Playgroud)
这将用于将用户提供的字体更改为 android 项目或应用程序。