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

时间:2015-07-08 20:07:19

标签: android text fonts keyboard

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

那么简单

android:keyTextFont="sans-serif"

3 个答案:

答案 0 :(得分:3)

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

将此类添加到您的代码中。

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。这些是外部字体文件,您可以使用它们覆盖键盘视图/标签。

将此应用程序名称添加到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>

答案 1 :(得分:0)

是的,可以通过重写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);
        }
    }
}

未经过测试的代码

答案 2 :(得分:0)

您必须创建一个扩展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/>