如何在 ANDROID STUDIO 中导入外部字体/字样?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24378668/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to import external font/ typeface in ANDROID STUDIO?
提问by S Zain Bukhari
I want to know how do I use an external font in Android Studio as there is no Assets folder. I have look for a helpful turtorial on internet but they all pretend to use Assets folder.
我想知道如何在 Android Studio 中使用外部字体,因为没有Assets 文件夹。我在互联网上寻找了一个有用的教程,但他们都假装使用 Assets 文件夹。
I created an asset folder myself in src/main
but Android Studio doesnt recognie getAssets()
.
我自己创建了一个资产文件夹,src/main
但 Android Studio 无法识别getAssets()
。
回答by MD Furkanul Islam
Go on your Project: app-->src-->main
继续你的项目:app-->src-->main
Create a assets folder like this:
创建一个资产文件夹,如下所示:
|assets
|-----------------fonts
|-------------------font.ttf
|java
|res
AndroidManifest.xml
and then use
然后使用
Typeface face=Typeface.createFromAsset(getAssets(),"fonts/digital.ttf");
txtV.setTypeface(face);
回答by Namrata
If you have custom font then use following code:
如果您有自定义字体,请使用以下代码:
TextView tv=(TextView)findViewById(R.id.custom);
Typeface face=Typeface.createFromAsset(getAssets(),"fonts/Verdana.ttf");
tv.setTypeface(face);
Also place your font file in assets/fonts folder and follow the instructionsfrom here.
还将您的字体文件放在 assets/fonts 文件夹中,然后按照此处的说明进行操作。
NOTE:You have to make asset folder by yourself
注意:您必须自己制作资产文件夹
回答by hanilozmen
Put your font files (e.g customfont.tff and customfont_bold.tff) under the folder app>src>res>font> (Note: If it is not present, create font folder)
将您的字体文件(例如 customfont.tff 和 customfont_bold.tff)放在文件夹 app>src>res>font> 下(注意:如果它不存在,请创建字体文件夹)
Additionally, create a file named fonts.xml inside the same folder with the following content:
此外,在同一个文件夹中创建一个名为 fonts.xml 的文件,内容如下:
<font-family xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<font
app:fontStyle="normal"
app:fontWeight="700"
app:font="@font/customfont"/>
<font
app:fontStyle="normal"
app:fontWeight="700"
app:font="@font/customfont_bold"/>
</font-family>
Then, edit the file app>src>res>values>styles.xml to apply a default font for the entire app.
然后,编辑文件 app>src>res>values>styles.xml 为整个应用程序应用默认字体。
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:fontFamily">@font/customfont</item>
</style>
If you want to change the font of an individual UI element:
如果要更改单个 UI 元素的字体:
<TextView
android:fontFamily="@font/customfont"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textColor="@color/black"
android:textSize="18sp"
android:text="Some text"
/>
NOTE: This method is valid for API Level 16+ (For more info: https://developer.android.com/guide/topics/ui/look-and-feel/fonts-in-xml)
注意:此方法适用于 API 级别 16+(更多信息:https: //developer.android.com/guide/topics/ui/look-and-feel/fonts-in-xml)
回答by akash ingle
if you are having an error with "getAssets()" method then you can use following way. in assets folder puts font family
如果您在使用“getAssets()”方法时遇到错误,那么您可以使用以下方式。在资产文件夹中放置字体系列
Typeface getFace=Typeface.create("OpenSans",3);
textView = (TexView) findViewById(R.id.textView);
textView.setTypeface(getFace);
回答by ONE
If you'e tried both the res/asset/font and main/asset font folders and you've tried different fonts, and it failed to work -- it's likely to be an Android Studio bug.
如果您尝试了 res/asset/font 和 main/asset 字体文件夹,并且尝试了不同的字体,但它无法工作——这很可能是 Android Studio 的错误。
I had the same problem but I solved it by importing my font to an online font editor (search for pentacom font editor) and exporting the font and saving it to a new ttf file. The resulting font will be lower in resolution but it worked for me.
我遇到了同样的问题,但我通过将我的字体导入在线字体编辑器(搜索 pentacom 字体编辑器)并导出字体并将其保存到新的 ttf 文件来解决它。生成的字体的分辨率会较低,但对我有用。
There might be other online font editors/exporters you can try.
您可能可以尝试其他在线字体编辑器/导出器。
回答by Joseph Ali
According to android developers : Android 8.0 (API level 26) introduces a new feature, Fonts in XML, which lets you use fonts as resources. You can add the font file in the res/font/ folder to bundle fonts as resources. These fonts are compiled in your R file and are automatically available in Android Studio. You can access the font resources with the help of a new resource type, font. For example, to access a font resource, use @font/myfont, or R.font.myfont.
根据 android 开发人员的说法:Android 8.0(API 级别 26)引入了一项新功能,即 XML 中的字体,它允许您将字体用作资源。您可以在 res/font/ 文件夹中添加字体文件,将字体捆绑为资源。这些字体在您的 R 文件中编译,并在 Android Studio 中自动可用。您可以在新资源类型 font 的帮助下访问字体资源。例如,要访问字体资源,请使用 @font/myfont 或 R.font.myfont。
for more details : https://developer.android.com/guide/topics/ui/look-and-feel/fonts-in-xml.html
更多详情:https: //developer.android.com/guide/topics/ui/look-and-feel/fonts-in-xml.html