【问题标题】:How To Apply Font in Custom Android Keyboard如何在自定义 Android 键盘中应用字体
【发布时间】:2017-10-05 23:12:02
【问题描述】:

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

我在KeyboardView 类中使用字体,但这不适合我。

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

【问题讨论】:

标签: android typeface


【解决方案1】:

一种解决方案是使用 keboardView.java 代替 android.inputmethodservice.KeyboardView。

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

另一种解决方案:

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

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

  }
}

在这里您可以看到添加了一些字体/字体名称。这些是您可以用来覆盖到键盘视图/标签中的外部字体文件。

将此应用程序名称添加到 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 项目或应用程序提供的字体。

【讨论】:

    【解决方案2】:

    试试这个

    • 您必须在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);
            }
    }
    

    【讨论】:

      猜你喜欢
      • 2012-08-19
      • 2016-09-27
      • 1970-01-01
      • 2019-05-26
      • 1970-01-01
      • 2017-02-21
      • 1970-01-01
      • 2015-11-10
      • 2013-01-18
      相关资源
      最近更新 更多