我开发了一个 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 自定义键盘的输出字体吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!