开发者

Where should I put the "ttf" files in an Android project?

I'm trying to use fonts in my project using Typeface. I copied the "font.ttf" into a folder named "fonts" inside "assets" folder. But eclipse do not allow to compile it. It shows an error on fonts folder. What s开发者_如何学Gohould I do?

The error I get is:

invalid resource directory name     fonts   /NoRats/assets  line 1  Android AAPT Problem


Please check this link, it may be helpful to you.


OR


use below code.

Typeface tf = Typeface.createFromAsset(this.getAssets(),"fonts/LATINWD.TTF");
txt1.setTypeface(tf);

.ttf file is in --> assets/fonts/LATINWD.TTF

Please first clean your project then check again.

Happy Coding.


Put your Font File in Assest Folder,

and in use Bellow Code for access.

Typeface tf=Typeface.createFromAsset(getAssets(),"fonts/Century Gothic.ttf");


textview=(TextView)findViewById(R.id.textviewone);
       textview.setTypeface(tf);

ttf file is in --> assets/fonts/Century Gothic.ttf


@ Android studio within Please check this code may be helpful to you

app/src/main/assets/your ttf file.

@ Add this code in activity.

Typeface typeface = Typeface.createFromAsset(getAssets(), ""); txt.setTypeface = (typeface)

@ Add this code in Adapter

Typeface typeface = Typeface.createFromAsset(context.getAssets(), ""); txt.setTypeface = (typeface)


Thank you everybody for answers. But i managed to make it work by doing following

"clean your project with Project menu > clean tab then build your project

If it's still unsolved, configure build path by right click on project > build path > android > restore default

I hope this is helpful"

answer found at Error in Android project name in Eclipse


As of Android Studio 1.5.1 you can:

  1. Right click on your app directory
  2. New > Folder (this is near the bottom of the list and is easy to miss) > Assets Folder
  3. In most cases you can leave the folder location as the default > Finish
  4. Move your files into the newly created assets folder


Since font organization and utilization has changes in Android 8 (API 26), it is now recommended to put your fonts into /res/font/ directory and access them with:

(Typeface) ResourcesCompat.getFont(context, R.font.your_font_bold);

which will return a Typeface object you needed.


You should put fonts inside this path

\app\src\main\assets\fonts

[Note : Create a new directory(Folder) inside the assests directory in the above patha i have created ]

Then put your ttf font file inside the fonts directory . You can set your font like this way :

Typeface anyName  = Typeface.createFromAsset(Context,"fonts/yourFontFileName.ttf");

simpleTextView.setTypeface(anyName);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜