在片段初始化字样 [英] Initialize Typeface in a fragment

查看:165
本文介绍了在片段初始化字样的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样一行:

Typeface font = Typeface.createFromAsset(getAssets(), "fonts/Delius-Regular.ttf");

getAssets()参数似乎带来了一些错误,但强调了与可怕的红色线条,它说:

but the getAssets() parameter seems to bring some error, it is underlined with scary red lines, it says

该方法getAssets()是未定义的类型ProfileFragment

The method getAssets() is undefined for the type ProfileFragment

ProfileFragment是我班的名称,它扩展片段。

ProfileFragment is the name of my class and it extends Fragment.

注意:的字体样式也对资产的文件夹

NOTE : The font style is also on the assets folder.

推荐答案

您不能得到 getAssets()直接从一个片段。你必须使用 getActivity()。getAssets(),而不是只使用 getAssets()

You cannot get getAssets() from a fragment directly. You have to use getActivity().getAssets() instead of using only getAssets().

使用此

Typeface font = Typeface.createFromAsset(getActivity().getAssets(), "fonts/Delius-Regular.ttf");

而不是

Typeface font = Typeface.createFromAsset(getAssets(), "fonts/Delius-Regular.ttf");


了解更多关于为Android片段设置自定义字体

这篇关于在片段初始化字样的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆