2017-10-05 76 views
1

我想为Android实现自定义keyboard,我想在keyboard上应用字体。如何在自定义Android键盘上应用字体

我在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); 
} 
+0

一个选项添加这些行的是,创建键盘自定义的文本视图和它的输入字段。 –

+0

[我可以更改Android自定义键盘的输出字体吗?](https://stackoverflow.com/questions/34332318/can-i-change-the-output-font-of-an-android-custom -键盘) –

回答

1

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

您还需要将paint.setTypeface(Typeface.DEFAULT_BOLD)更改为paint.setTypeface(我的字体),并且必须将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项目或应用程序。

1

尝试这个

  • 你已经在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); 
     } 
}