我可以更改Android自定义键盘的输出字体吗? [英] Can I change the output font of an Android custom keyboard?
问题描述
$ b
字体也不在android设备中,所以我们必须从正在开发键盘的同一个应用程序向外部提供字体。
创建一个简单的类名为
FontOverride
import java.lang.reflect.Field;
导入android.content.Context;
导入android.graphics.Typeface;
public final class FontsOverride {
public static void setDefaultFont(Context context,
String staticTypefaceFieldName,String fontAssetName){
final Type Regular = Typeface。 createFromAsset(context.getAssets(),
fontAssetName);
replaceFont(staticTypefaceFieldName,regular);
protected static void replaceFont(String staticTypefaceFieldName,$ b $ 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();
$ b $ / code $ / pre
现在创建另一个类来覆盖
$ b
应用程序
<$ p $ ();} $ {code $} 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 Manifest文件中的应用程序名称
android:name =。Application
这会将用户提供的字体更改为android项目或应用程序。
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.
create a simple class named
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();
}
}
}
now create another class to override the fonts named
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");*/
}
}
now add this typeface to style in android style file at values folder
<item name="android:typeface">monospace</item>
At last mention the Application name at android Manifest file inside application tag
android:name=".Application"
This will work to change the user provided font to the android project or application.
这篇关于我可以更改Android自定义键盘的输出字体吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!