我正在尝试使用字体导入自定义加载的字体。为了做到这一点,我必须访问我的 Assets 。当我通过扩展 Activity 的 MainActivity 类运行以下代码时,它不会引发错误。
public class MainActivity extends Activity {
Typeface font = Typeface.createFromAsset(getAssets(), "fonts/EuphoriaScript.otf");
}
当我通过 View 类运行它时,它在 getAssets() 方法上引发异常。我猜这是因为它需要以不同的方式访问。我怎样才能做到这一点?
谢谢
最佳答案
AssetManager附在您的申请包中。因此,要访问它,您只需要 Context 。每个 View 都包含对其 Context 的引用因此,要从 View 类获取 AssetManger,只需执行以下操作:
AssetManager assetManger = getContext().getAssets();
关于java - 如何从 View 类访问我的 Assets ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24107142/