|
发表于 2025-8-5 20:00:57
|
显示全部楼层
不知干啥,修改了一下,不知能不能用。
public void 自定义字体(String var1) {
if (var1 == null || var1.isEmpty()) {
Log.e("FontError", "字体路径不能为空");
return;
}
try {
Typeface newTypeface;
// 更安全的路径判断
if (new File(var1).isAbsolute()) {
if (!new File(var1).exists()) {
Log.e("FontError", "字体文件不存在: " + var1);
return;
}
newTypeface = Typeface.createFromFile(var1);
} else {
Context context = mainActivity.getContext();
if (context == null || context.getAssets() == null) {
Log.e("FontError", "上下文或Assets不可用");
return;
}
newTypeface = Typeface.createFromAsset(context.getAssets(), var1);
}
if (newTypeface == null) {
Log.e("FontError", "创建字体失败");
return;
}
this.typeface = newTypeface;
if (this.paintColor != null) {
this.paintColor.setTypeface(this.typeface);
} else {
Log.e("FontError", "paintColor未初始化");
}
} catch (Exception e) {
Log.e("FontError", "加载字体异常: " + e.getMessage());
// 可以设置回默认字体
this.typeface = Typeface.DEFAULT;
if (this.paintColor != null) {
this.paintColor.setTypeface(this.typeface);
}
}
} |
|