2017-10-05 38 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); 
     } 
} 
相關問題