你能以编程方式更改Android键盘文本字体吗? [英] Can you change the android keyboard text font programmatically?
问题描述
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 $ pre $ public final class Application扩展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清单文件应用程序名称中
$ p
$ $ $ $ $
$ android $ name =。Application
android:allowBackup =false
android:installLocation =internalOnly
android:label =@ string / ime_name
android:theme =@ style / AppTheme > .......
现在将上面的过期字体名称更新到您的样式中。基本主题或您在清单应用程序中使用的主题。
示例
<! - 应用程序主题。 - >
< style name =AppThemeparent =AppBaseTheme>
< item name =android:typeface> monospace< / item>
< / style>
To be frank the title says it all really, I'm trying to change an input method service, KeyboardView key font. Of course.. it's not as simple as
android:keyTextFont="sans-serif"
.
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();
}
}
}
add this class to your 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");*/
}
}
..... here you can see that the some fonts/fontname are added. These are the external font files which you can use to overrite into your keyboard view/labels.
add this Application name into the android manifest file application name
example
<application
android:name=".Application"
android:allowBackup="false"
android:installLocation="internalOnly"
android:label="@string/ime_name"
android:theme="@style/AppTheme" >.......
now update the above overrited font name into your style. base theme or the theme which you are using at your manifest application.
example
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<item name="android:typeface">monospace</item>
</style>
这篇关于你能以编程方式更改Android键盘文本字体吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!