I used this in my extension:
@SimpleFunction(description = "设置组件字体。fontName字体名以//开始表明字体在素材库")
public void setFontTypeface(AndroidViewComponent component, String fontName) {
TextView view = (TextView) component.getView();
Typeface tf = Typeface.DEFAULT;
if (fontName.startsWith("//")) {
fontName = fontName.substring(2);
if (isReplForm) {
String realpath;
String files = this.context.getExternalFilesDir(null).getPath();
if (Build.VERSION.SDK_INT >= 29) {
realpath = files + "/assets/" + fontName;
} else {
if (files.contains("wxbit")) {
realpath = files + "/assets/" + fontName;
} else {
realpath = files + "/AppInventor/assets/" + fontName;
tf = Typeface.createFromFile(realpath);
}
}
tf = Typeface.createFromFile(realpath);
} else {
tf = Typeface.createFromAsset(this.context.getAssets(), fontName);
}
} else {
if (fontName.toLowerCase().startsWith("file://")) {
fontName = fontName.substring(7);
}
tf = Typeface.createFromFile(fontName);
}
view.setTypeface(tf);
view.requestLayout();
}