简体   繁体   English

如何在android studio中创建自定义字体

[英]How to create custom fonts in android studio

I need to add new fonts to my project where do i create it in android studio 我需要在我的项目中添加新字体,我在android studio中创建它

Can someone advise if i create it in the correct directory in android studio? 有人可以建议我是否在android studio中的正确目录中创建它?

I tried putting the fonts folder same location as SRC folder but didn't work out 我尝试将fonts文件夹放在与SRC文件夹相同的位置但是没有用完

my code is 我的代码是

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

Is this correct? 这个对吗? please advise 请指教

if you use android studio , you should create assets folder from right click at 如果你使用android studio,你应该从右键单击创建资源文件夹

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

在此输入图像描述

choose destination to your assets folder default main 选择目标到您的资产文件夹默认主页

在此输入图像描述

create directory called fonts in assets then place your font 在资源中创建名为fonts的目录,然后放置您的字体

在此输入图像描述

|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 );

You need to place your font file at the root of your project, inside a folder called 'assets'. 您需要将字体文件放在项目的根目录中,位于名为“assets”的文件夹中。 You should end up with this structure : 你最终应该得到这样的结构:

src
|------------assets
    |-----------------fonts
        |-------------------font.ttf
|------------java
|------------libs
|------------res
AndroidManifest.xml

In android studio , the assets folder have to be placed under the " main " folder. android studio中assets文件夹必须放在“ main ”文件夹下。 Not under the "res" folder. 不在 “res”文件夹下。

Since Android Studio uses the new Gradle-based build system, you should be putting the "assets/" folder inside of the source sets. 由于Android Studio使用新的基于Gradle的构建系统,因此您应该将“assets /”文件夹放在源集中。

That was working for me: 这对我有用:

src/main/assets/fonts/examalpefont.ttf 的src / main /资产/字体/ examalpefont.ttf

You have to download custom font .ttf file. 您必须下载自定义字体.ttf文件。 In my case I downloaded din.ttf file. 在我的情况下,我下载了din.ttf文件。 put that in app->assets->fonts->din.ttf Write the code in MainActivity.java file 把它放在app-> assets-> fonts-> din.ttf中编写MainActivity.java文件中的代码

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

//that's it . //而已 。 Done! 完成!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM