Java 更改android自定义键盘的自定义键(标签)字体

Java 更改android自定义键盘的自定义键(标签)字体,java,android,unicode,fonts,keyboard,Java,Android,Unicode,Fonts,Keyboard,我在更改android自定义键盘自定义键的字体样式(非英语-Unicode)时遇到问题。 我遵循了与此类似的答案 对于单字符按钮,它似乎工作得很好。它会将整个应用程序的字体更改为新字体,包括键盘上的单字符键。 如果我想改变关键文本的大小,我可以使用下面两个条目 android:keyTextSize="25sp" // for single character keys android:labelTextSize="20sp" // for multiple character keys 但不

我在更改android自定义键盘自定义键的字体样式(非英语-Unicode)时遇到问题。 我遵循了与此类似的答案 对于单字符按钮,它似乎工作得很好。它会将整个应用程序的字体更改为新字体,包括键盘上的单字符键。 如果我想改变关键文本的大小,我可以使用下面两个条目

android:keyTextSize="25sp" // for single character keys
android:labelTextSize="20sp" // for multiple character keys
但不幸的是,上述链接中的方法仅适用于单字符密钥。有没有办法设置多个字符键的字体

例如,请参见下图: 第一个按钮有一些默认的系统字体,而第二个和第三个按钮有正确的字体

编辑: 读了Bhavita Lalwani之后,我开始思考

if (label != null) {
        // For characters, use large font. For labels like "Done", use small font.
        if (label.length() > 1 && key.codes.length < 2) {
            paint.setTextSize(mLabelTextSize);
            paint.setTypeface(Typeface.DEFAULT_BOLD);
        } else {
            paint.setTextSize(mKeyTextSize);
            paint.setTypeface(Typeface.DEFAULT);
                }
            }
因此,难看的解决方法是添加多个代码,并且只在需要时使用第一个代码

<Key android:codes="5001,1" android:keyLabel="AB" android:keyWidth="12%p" />

现在,每个带有多个代码的键都是用户默认的字体。
这一方法目前有效,(直到我找到一个合适的解决方案:)

我在创建印地语自定义键盘时遇到了类似的问题。(非英语Unicode)

那么,让我们来找出这种变化发生的原因

第701-709行

            if (label != null) {
            // For characters, use large font. For labels like "Done", use small font.
            if (label.length() > 1 && key.codes.length < 2) {
                paint.setTextSize(mLabelTextSize);
                paint.setTypeface(Typeface.DEFAULT_BOLD);
            } else {
                paint.setTextSize(mKeyTextSize);
                paint.setTypeface(Typeface.DEFAULT);
                    }
                }
现在在这个CustomKeyboardView类中,重写onDraw方法。在画布上绘制键盘和键时将调用此方法

@Override
public void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    Paint mpaint = new Paint();
    mpaint.setTypeface(Typeface.DEFAULT_BOLD); //to make all Bold. Choose Default to make all normal font
    mpaint.setTextSize(24); // in px


    List<Key> keys = getKeyboard().getKeys();
    for (Keyboard.Key key : keys) {

        if (key.label != null) {
            String keyLabel = key.label.toString();
            canvas.drawText(keyLabel, key.x + key.width, key.y + key.height, 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);
        }
    }
}
最后,就像我们用来制作键盘一样

     KeyboardView kv = (CustomKeyboardView) getLayoutInflater().inflate(R.layout.mainkeyboard, null); //mainkeyboard
     Keyboard  keyboard = new Keyboard(this, R.xml.hindi); //Your Keyboard Layout
     kv.setKeyboard(keyboard); //Set the keyboard
你可以走了


希望能有帮助:D

不幸的是,这对我来说并没有真正起作用。自定义键仍然显示错误的字体。但这给了我一个想法。请看我编辑的问题的第二部分。你确定它不起作用吗?它对我起作用了。然而,我不得不删除super.onDraw。(从我上面写的代码中),因为它显然是在使用代码集,并且在我可以单独绘制它们之前,使用粗体、默认值进行绘制。尝试从覆盖的onDraw中删除super.onDraw(从onDraw的实际实现中签出您可能需要的其他依赖项。)P.这是我的第一个答案。如果这对你有用的话,我向你道歉。
public class CustomKeyboardView extends KeyboardView {

public CustomKeyboardView(Context context, AttributeSet attrs) {
    super(context, attrs);
}
@Override
public void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    Paint mpaint = new Paint();
    mpaint.setTypeface(Typeface.DEFAULT_BOLD); //to make all Bold. Choose Default to make all normal font
    mpaint.setTextSize(24); // in px


    List<Key> keys = getKeyboard().getKeys();
    for (Keyboard.Key key : keys) {

        if (key.label != null) {
            String keyLabel = key.label.toString();
            canvas.drawText(keyLabel, key.x + key.width, key.y + key.height, 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);
        }
    }
}
<?xml version="1.0" encoding="utf-8"?>
<resources>
  <dimen name="custom_text_size">25sp</dimen>
</resources>
mpaint.setTextSize(getResources().getDimensionPixelSize(R.dimen.custom_text_size));
     KeyboardView kv = (CustomKeyboardView) getLayoutInflater().inflate(R.layout.mainkeyboard, null); //mainkeyboard
     Keyboard  keyboard = new Keyboard(this, R.xml.hindi); //Your Keyboard Layout
     kv.setKeyboard(keyboard); //Set the keyboard