【问题标题】:How can I access my Assets from my View class?如何从我的 View 类访问我的资产?
【发布时间】:2014-06-08 14:30:13
【问题描述】:

我正在尝试使用字体导入自定义加载的字体。为此,我必须访问我的资产。当我通过扩展 Activity 的 MainActivity 类运行以下代码时,它不会引发错误。

public class MainActivity extends Activity {
Typeface font = Typeface.createFromAsset(getAssets(), "fonts/EuphoriaScript.otf"); 
}

当我通过 View 类运行它时,它会在 getAssets() 方法上引发异常。我猜这是因为它需要以不同的方式访问。我该怎么做?

谢谢

【问题讨论】:

  • 我的问题是什么错误?
  • 你确定你得到一个例外吗?不是编译错误吗?

标签: java android fonts assets typeface


【解决方案1】:

AssetManager 已附加到您的应用程序包中。因此,要访问它,您只需要一个Context。每个视图都包含对其Context 的引用,因此要从视图类中获取 AssetManger,只需执行以下操作:

AssetManager assetManger = getContext().getAssets();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-01
    • 1970-01-01
    相关资源
    最近更新 更多