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

Can you change the android keyboard text font programmatically?

坦率地说,标题说明了一切,我正在尝试更改输入法服务、KeyboardView 键字体。当然..它不像

那么简单

android:keyTextFont="sans-serif"

是的,可以通过重写 KeyboardView 的 onDraw() 方法以务实的方式完成。

下面是某人在键上方添加文本的示例:Customize the appearance of a <Key>

要更改文本字体代码将是这样的:

public class MyKeyboardView extends KeyboardView {
@Override
public void onDraw(Canvas canvas) {
    Paint paint = new Paint();
    paint.setTextAlign(Paint.Align.CENTER);
    Typeface font = Typeface.createFromAsset(context.getAssets(),
        "fonts/Hippie.otf"); //Insert your font here.
    paint.setTypeface(font);

    List<Key> keys = getKeyboard().getKeys();
    for(Key key: keys) {
        if(key.label != null)
            canvas.drawText(key.label.toString(), key.x, key.y, paint);
        }
    }
}

代码未测试

您必须创建一个新的 class 来扩展 KeyboardView。

package com.example.xyz;
...
...
public class CustomKeyboardView extends KeyboardView{
    @Override
    public void onDraw(Canvas canvas) {
        Paint mPaint = new Paint();
            mPaint.setTextAlign(Paint.Align.CENTER);
            mPaint.setTextSize(40);
            mPaint.setColor(Color.BLACK);

            Typeface font = Typeface.createFromAsset(mContext.getAssets(),"normal_font.ttf");
            mPaint.setTypeface(font);

            if (key.label != null) {
                String keyLabel = key.label.toString();
                if (caps) {
                    keyLabel = keyLabel.toUpperCase();
                }
                canvas.drawText(keyLabel, key.x + (key.width / 2),
                        key.y + (key.height / 2) , mPaint);
            } else if (key.icon != null) {
                key.icon.setBounds(key.x, key.y, key.x + key.width, key.y + key.height);
                key.icon.draw(canvas);
            }
    }
}

其中 .ttf 文件是项目 Assets 文件夹中的字体文件。

现在,在键盘布局 (.xml) 文件中,将 <KeyboardView/> 替换为 <com.example.xyz.CustomKeyboardView/>

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 添加到您的代码中。

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");*/
  }
}

..... 在这里你可以看到添加了一些fonts/fontname。这些是您可以用来覆盖到键盘的外部字体文件 view/labels.

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

例子

 <application
    android:name=".Application"
    android:allowBackup="false"
    android:installLocation="internalOnly"
    android:label="@string/ime_name"
    android:theme="@style/AppTheme" >.......

现在将上面覆盖的字体名称更新为您的样式。基本主题或您在清单应用程序中使用的主题。

例子

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