gpt4 book ai didi

android - 自定义字体未在设备上显示

转载 作者:行者123 更新时间:2023-11-29 02:32:11 25 4
gpt4 key购买 nike

我正在尝试按照关于此 site 的“XML 中的字体”教程添加自定义字体.我严格按照教程进行操作,并检查了很多次以找到我错过的东西,但我就是看不到。我添加的字体是一个ttf文件。

在设计器中,我可以选择我的字体系列,TextView 中的文本会更改为我添加的自定义字体。但是,当我在我的设备上运行该应用程序时,文本默认为常规字体。 Android Studio 中包含的字体不会发生这种情况。

其他问题:当我尝试另一种自定义字体时,TextView 中的文本更改为自定义字体,但文本本身(内容)也更改为一些乱码。这是字体错误还是其他原因的迹象?

sv_regular.xml

<?xml version="1.0" encoding="utf-8"?>
<font-family xmlns:android="http://schemas.android.com/apk/res/android">
<font
android:fontStyle="normal"
android:fontWeight="400"
android:font="@font/sv_font_regular" />

</font-family>

TextView

<TextView
android:id="@+id/toolbar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:fontFamily="@font/sv_regular"
android:text="@string/app_name" />

最佳答案

您可以通过编程方式执行此操作

在 Assets 文件夹中添加外部字体

<强>1。转到(项目文件夹)

<强>2。然后 app>src>main

<强>3。在主文件夹中创建文件夹“assets>fonts”。

<强>4。将您的“abc.ttf”放入字体文件夹。

TextView tx = (TextView)findViewById(R.id.textview1);

Typeface custom_font = Typeface.createFromAsset(getAssets(),"fonts/abc.ttf");

tx.setTypeface(custom_font);

关于android - 自定义字体未在设备上显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49031923/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com