【问题标题】:How can i save font typeface in SharedPreferences?如何在 SharedPreferences 中保存字体字体?
【发布时间】:2014-06-24 13:02:09
【问题描述】:

我正在尝试将字体字体保存在 SharedPreferences 中。但我无法实现它,请帮助我。谢谢 这是我的代码。

阅读..

SharedPreferences fontSP = getActivity().getSharedPreferences("PREFSFONT", Context.MODE_WORLD_READABLE);


m_txTitle.setTypeface(fontSP.getString("fontValue", SettingsABC.getTypeface(fontStyle));

保存..

fontStyle = Typeface.createFromAsset(getAssets(), "caligula.ttf");
                    SharedPreferences fontSP = getSharedPreferences("PREFSFONT", MODE_WORLD_READABLE);
                    SharedPreferences.Editor bgEditor = fontSP.edit();
                    bgEditor.putString("fontValue", fontStyle.toString());
                    bgEditor.commit();

【问题讨论】:

    标签: android sharedpreferences


    【解决方案1】:

    您将 Font 对象的 toString() 保存到首选项中,然后尝试使用它来恢复该值,我 99.9% 肯定这不会起作用。 toString() 不是序列化机制。

    相反,您应该编写文件名并使用该文件名进行恢复。

    bgEditor.putString("fontValue", "caligula.ttf"); //Use the filename here
    

    然后恢复:

    fontStyle = Typeface.createFromAsset(getAssets(), fontSP.getString("fontValue", SettingsABC.getTypeface(fontStyle));
    m_txTitle.setTypeface(fontStyle);
    

    或类似的东西。

    【讨论】:

    • 我从 bgEditor.putString(string,string) 开始添加到 string()。以下陈述也是错误的。 fontStyle = Typeface.createFromAsset(getAssets(), fontSP.getString("fontValue", SettingsABC.getTypeface(fontStyle));
    • 但是您不想读取字体,您想从文件中读取。
    • 不幸的是,Typeface.createFromAsset 造成内存泄漏:stackoverflow.com/questions/16901930/…。那么到底有没有办法保存字体,而不仅仅是文件名?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-08
    • 2021-07-04
    • 1970-01-01
    相关资源
    最近更新 更多