我可以更改 Android 自定义键盘的输出字体吗?
Can I change the output font of an Android custom keyboard?
我开发了一个 android 自定义键盘。我需要更改实际使用 unicode 打印的输出文本的字体样式。
如何在不更改设备默认字体的情况下,在整个设备的任何位置更改键盘文本输出的字体样式?
字体也不在 android 设备中,因此我们必须从开发键盘的同一应用程序外部提供字体。
更改应用程序内的字体样式。
创建一个名为
的简单 class
FontOverride
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();
}
}
}
现在创建另一个 class 来覆盖名为
的字体
Application
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 样式文件中的样式
<item name="android:typeface">monospace</item>
最后在 android 应用程序标签内的清单文件中提及应用程序名称
android:name=".Application"
这将用于将用户提供的字体更改为 android 项目或应用程序。
我开发了一个 android 自定义键盘。我需要更改实际使用 unicode 打印的输出文本的字体样式。
如何在不更改设备默认字体的情况下,在整个设备的任何位置更改键盘文本输出的字体样式?
字体也不在 android 设备中,因此我们必须从开发键盘的同一应用程序外部提供字体。
更改应用程序内的字体样式。
创建一个名为
的简单 classFontOverride
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();
}
}
}
现在创建另一个 class 来覆盖名为
的字体Application
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 样式文件中的样式
<item name="android:typeface">monospace</item>
最后在 android 应用程序标签内的清单文件中提及应用程序名称
android:name=".Application"
这将用于将用户提供的字体更改为 android 项目或应用程序。