初始化片段中的字体

时间:2013-10-29 08:32:25

标签: android android-fragments fragment typeface

我有这句话:

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

但是getAssets()参数似乎带来了一些错误,它带有可怕的红线下划线,它说

  

对于类型ProfileFragment

,方法getAssets()未定义

ProfileFragment是我的类的名称,它扩展了Fragment。

注意:字体样式也位于资源文件夹中。

1 个答案:

答案 0 :(得分:6)

您无法直接从片段中获取getAssets()。您必须使用getActivity().getAssets(),而不是仅使用getAssets()


使用此

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

而不是

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

详细了解Set custom font for Android fragments