您可以以编程方式更改 android 键盘文本字体吗?

时间:2023-01-31
本文介绍了您可以以编程方式更改 android 键盘文本字体吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

坦率地说,标题确实说明了一切,我正在尝试更改输入法服务,KeyboardView 键字体.当然..不是那么简单的

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.

将此应用程序名称添加到 android 清单文件应用程序名称中

add this Application name into the android manifest file application name

例子

 <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.

例子

 <!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
    <item name="android:typeface">monospace</item>
</style>

这篇关于您可以以编程方式更改 android 键盘文本字体吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:隐藏在键盘下的 SKStoreReviewController 按钮 下一篇:Android 键盘 - 开始小写

相关文章

最新文章