以编程方式设置自定义字体

Mar*_*lic -1 java android textview android-typeface

非常坦率的.它似乎不想为我工作.我得到的错误getAssets()是红色,它说Cannot resolve method 'getAssets()'.

    text_View = (TextView) this.view.findViewById(R.id.textDesigner);
    Typeface myTypeface = Typeface.createFromAsset(getAssets(), "fonts/Calligraffitti-Regular.ttf");
    text_View.setTypeface(myTypeface);
Run Code Online (Sandbox Code Playgroud)

我的字体的路径是: C:\Users\Joe\AndroidStudioProjects\my-app\app\src\main\assets\fonts\Calligraffitti-Regular.ttf

这是怎么回事?

Vas*_*las 5

尝试调用片段的活动,然后调用资产

getActivity().getAssets()
Run Code Online (Sandbox Code Playgroud)