如何在自定义 Android 键盘中应用字体

How To Apply Font in Custom Android Keyboard

我想为 android 实现自定义 keyboard,我想在 keyboard 上应用字体。

我在 KeyboardView class 中使用字体,但这对我不起作用。

 @Override
public void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    Paint paint = new Paint();
    paint.setTextAlign(Paint.Align.CENTER);
    int scaledSize = getResources().getDimensionPixelSize(
            R.dimen.alternate_key_label_size);
    Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "fonts/Nastaleeq.ttf");
    paint.setTypeface(tf);
    paint.setTextSize(scaledSize);
    paint.setColor(Color.WHITE);
}

一个解决方案是使用keboardView.java而不是android.inputmethodservice.KeyboardView。

您还需要将 paint.setTypeface(Typeface.DEFAULT_BOLD) 更改为 paint.setTypeface(我的字体),并且您必须将 attrs.xml 添加到您的项目中。

另一种解法:

更改应用程序内的字体样式。 创建一个名为 FontOverride.

的简单 class
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/Nastaleeq.ttf");
    FontsOverride.setDefaultFont(this, "MONOSPACE", "fonts/GeezEdit.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>

这将用于更改用户为 android 项目或应用程序提供的字体。

试试这个

  • 您必须在 onDraw
  • 中添加这些行

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);
        }
}