簡體   English   中英

Softkeyboard的Keylabel更改文本大小和KeyText字體

[英]Keylabel of Softkeyboard to change text size and font of keytext

我要自定義軟鍵盤。 我想更改keylabel的keytext的文本大小和字體。 我嘗試了很多方法,但鍵盤沒有任何變化。

您可以實現KeyboardView ,然后可以自定義鍵盤設計中的幾乎所有內容。

要更改文本大小,可以設置屬性android:keyTextSize

不幸的是,這不是更改xml中字體樣式的方法,您將需要以編程方式從KeyboardView擴展類。 在這種情況下,我建議菲利普回答

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

另一種方法是更改​​應用程序的字體,例如Ankush Bist說

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

在這里您可以看到另一個類似的問題: 鏈接

最后,您可以像我在這里一樣完全自定義xml布局

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM