【发布时间】:2020-01-15 00:05:08
【问题描述】:
我想自定义软键盘。我想更改 keylabel 的 keytext 的文本大小和字体。我尝试了很多方法,但键盘没有变化。
【问题讨论】:
标签: android android-softkeyboard android-fonts
我想自定义软键盘。我想更改 keylabel 的 keytext 的文本大小和字体。我尝试了很多方法,但键盘没有变化。
【问题讨论】:
标签: android android-softkeyboard android-fonts
您可以实现KeyboardView,然后您可以自定义键盘设计中的几乎所有内容。
要更改文本大小,您可以设置属性android:keyTextSize。
不幸的是,这不是更改 xml 中字体样式的方法,您需要以编程方式从 KeyboardView 扩展一个类。在这种情况下,我建议Phillip's answer:
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 said:
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();
}
}
}
在这里你可以看到另一个类似的问题:link
最后你可以像我here那样完全自定义一个xml布局
【讨论】: