How to create custom fonts in android studio

2020-04-11 20:32发布

I need to add new fonts to my project where do i create it in android studio

Can someone advise if i create it in the correct directory in android studio?

I tried putting the fonts folder same location as SRC folder but didn't work out

my code is

Typeface fontRobo = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Black.ttf");
viewTotalValue.setText(total.toString());

Is this correct? please advise

5条回答
▲ chillily
2楼-- · 2020-04-11 20:57

In android studio, the assets folder have to be placed under the "main" folder. Not under the "res" folder.

查看更多
三岁会撩人
3楼-- · 2020-04-11 21:11

You have to download custom font .ttf file. In my case I downloaded din.ttf file. put that in app->assets->fonts->din.ttf Write the code in MainActivity.java file

    TextView tx = (TextView)findViewById(R.id.textView1);
    Typeface custom_font = Typeface.createFromAsset(getAssets(), "fonts/din.ttf");
    tx.setTypeface(custom_font);

//that's it . Done!

查看更多
Juvenile、少年°
4楼-- · 2020-04-11 21:14

You need to place your font file at the root of your project, inside a folder called 'assets'. You should end up with this structure :

src
|------------assets
    |-----------------fonts
        |-------------------font.ttf
|------------java
|------------libs
|------------res
AndroidManifest.xml
查看更多
小情绪 Triste *
5楼-- · 2020-04-11 21:17

if you use android studio , you should create assets folder from right click at

app ---> New --->Folder --->Assets Folder

enter image description here

choose destination to your assets folder default main

enter image description here

create directory called fonts in assets then place your font

enter image description here

|assets

    |-----------------fonts

        |-------------------Roboto-Black.ttf

|java

|res

AndroidManifest.xml

finally use this code

Typeface fontRobo = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Black.ttf");
viewTotalValue.setText(total.toString()); 
viewTotalValue.setTypeface(fontRobo );
查看更多
Summer. ? 凉城
6楼-- · 2020-04-11 21:17

Since Android Studio uses the new Gradle-based build system, you should be putting the "assets/" folder inside of the source sets.

That was working for me:

src/main/assets/fonts/examalpefont.ttf

查看更多
登录 后发表回答