我想知道在Android中如何使用外部字体,因为没有资产文件夹。我已经在网上寻找一个有用的图文,但他们都假装使用资产文件夹。
我自己在src/main中创建了一个资产文件夹,但是Android没有识别getAssets()。
发布于 2014-12-19 10:57:30
继续你的项目:app->src->main
创建一个资产文件夹,如下所示:
|assets
|-----------------fonts
|-------------------font.ttf
|java
|res
AndroidManifest.xml然后使用
Typeface face=Typeface.createFromAsset(getAssets(),"fonts/digital.ttf");
txtV.setTypeface(face);发布于 2014-06-24 05:18:26
如果您有自定义字体,请使用以下代码:
TextView tv=(TextView)findViewById(R.id.custom);
Typeface face=Typeface.createFromAsset(getAssets(),"fonts/Verdana.ttf");
tv.setTypeface(face);还可以将字体文件放在“资产/字体”文件夹中,然后从这里开始使用使用说明。
注意:您必须自己制作资产文件夹
发布于 2019-10-01 14:45:26
把你的字体文件(例如customfont.tff和customfont_bold.tff)放在app>src>res>font>文件夹下(注意:如果它不存在,创建字体文件夹)
此外,在同一个文件夹中创建一个名为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>然后,编辑文件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>如果要更改单个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"
/>注意:此方法对于API级16+有效(获得更多信息:https://developer.android.com/guide/topics/ui/look-and-feel/fonts-in-xml)
https://stackoverflow.com/questions/24378668
复制相似问题