Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
159 views
in Technique[技术] by (71.8m points)

Android Typeface createFromAsset

I Have a Custom View which draws text onto the Canvas. I want to change the font to a font stored in the assets folder.

I am using Android Studio so I created a folder src/main/assets and placed my ttf files in there.

Paint txt = new Paint()
Typeface font = Typeface.createFromAsset(getAssets(), "robotobold.ttf");
txt.setTypeface(font);

Problem is Android Studio doesn't recognize getAssets() inside my Custom View, however, it recognizes it inside my Activity. I have tried passing Typeface through from my Activity but when I do it it doesn't change the font.

question from:https://stackoverflow.com/questions/18436703/android-typeface-createfromasset

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can use your View's getContext() method to get the current Context, then use it to get the assets:

Typeface font = Typeface.createFromAsset(getContext().getAssets(), "robotobold.ttf");

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...