【问题标题】:Android TextView Set and Get TypeFaceAndroid TextView 设置和获取 TypeFace
【发布时间】:2017-10-23 10:43:52
【问题描述】:

我需要存储 textView 的粗体、斜体和字体系列并从 DB 中检索它们:

所以,我从 TextView 和
后来我试图把它放回去。但是我遇到了崩溃,甚至没有显示任何错误。

这样做的正确方法是什么?

Gson GsonPkjVar = new Gson();
JsonObject textViewJsnVar = new JsonObject();

|*|将字体转换为 Gson 字符串并存储在 JsonObject 中:

textViewJsnVar.addProperty("TxtFntKey", GsonPkjVar.toJson(textViewVar.getTypeface()));

|*|将 Gson 字符串从 JsonObject 转换回字体:

textViewVar.setTypeface(GsonPkjVar.fromJson(textViewJsnVar.get("TxtFntKey").getAsString(), Typeface.class));

【问题讨论】:

  • 你到底为什么要这么做?
  • 为什么不保存字体名称,然后在运行时加载,
  • 如何动态获取字体名称?比如 .getTypeface().getFontFamily()

标签: java android fonts textview typeface


【解决方案1】:

改用类型标记

Type type=new TypeToken<Typeface>(){}.getType();
Typeface typeface=GsonPkjVar.fromJson(textViewJsnVar.get("TxtFntKey").getAsString(),type);

【讨论】:

  • 这对我有帮助。遇到同样的崩溃。你能解释一下我的方法和 TypeToken 方法有什么区别吗?
  • post crash也可以。Type是指定要反序列化数据的类型。
  • 正如我在问题中提到的那样,它甚至没有显示任何错误就崩溃了
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-05
  • 1970-01-01
  • 1970-01-01
  • 2018-04-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多