马合 发表于 2024-7-23 17:06:58

万年历类库

本帖最后由 马合 于 2024-7-23 17:08 编辑

public void 自定义字体(String var1) {
      if (var1.startsWith("/")) {
         this.typeface = Typeface.createFromFile(var1);
      } else {
         this.typeface = Typeface.createFromAsset(mainActivity.getContext().getAssets(), var1);
      }

      this.paintColor.setTypeface(this.typeface);
   }
错误在什么地方,求教大师指点.{:4_102:}

lishihai 发表于 2024-7-24 09:26:07

高深,看不懂,{:4_108:}

美丽新世界 发表于 2024-8-20 20:02:02

你想写 排八字软件?

jmjjjj 发表于 2024-8-26 07:46:42

看看看看

creator2 发表于 2025-7-26 20:13:01

论坛有你更精彩~

2297416568 发表于 2025-8-5 20:00:57

creator2 发表于 2025-7-26 20:13
论坛有你更精彩~

不知干啥,修改了一下,不知能不能用。
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);
      }
    }
}

mandur 发表于 2025-8-6 00:33:09

感谢楼主
页: [1]
查看完整版本: 万年历类库