Xamarin.Android-android:fontFamily不起作用 [英] Xamarin.Android - android:fontFamily doesn't work
问题描述
我需要使用android:fontFamily更改textView字体. 问题是它给了我错误,我不知道为什么
I need to change textView font using android:fontFamily. The problem is that it gives me errors and I have no idea why
我的代码:
<TextView
android:text="Arial font"
android:textSize="8pt"
android:fontFamily="@font/arial"
android:layout_marginTop="4pt"/>
我实际上尝试了所有操作:在Assets文件夹中添加Arial.ttf,在Assets中创建了字体文件夹,尝试更改属性,尝试在android:fontFamily中使用"arial.ttf"而不是"arial",但没有任何效果
I tried literally everything: added Arial.ttf on Assets folder, created a font folder in Assets, tried to change properties, tried to use "arial.ttf" instead of "arial" in android:fontFamily, but nothing has worked.
推荐答案
仅API-26和更高版本提供支持:
-
将字体文件添加到
Resources/font
目录对于 API-26 + ,仅支持使用
android:fontFamily
很好:For API-26+ only support using
android:fontFamily
is fine:android:fontFamily="@font/tastysushi"
-
添加
Xamarin.Android.Support.Compat
程序包(v26或更高版本)
Add the
Xamarin.Android.Support.Compat
package (v26 or higher)
如果在AXML布局中使用
fontFamily
,则添加Xamarin.Android.Support.v7.AppCompat
包(v26 +)(否则,您可以以编程方式分配字体系列并使用Activity
子类)Add the
Xamarin.Android.Support.v7.AppCompat
package (v26+) if you are usingfontFamily
in your AXML layouts (otherwise you can programmatically assign the font family and useActivity
subclasses)您的活动必须是
AppCompatActivity
的子类,才能使基于layout/AXML的app:fontFamily
属性起作用Your activity must be a subclass of
AppCompatActivity
in order for the layout/AXML-basedapp:fontFamily
attribute to work在
Resources/font
目录中添加字体文件|-Resources | |-font | | |-tastysushi.ttf
-
-
可选,但很方便,可以在
Resources/font
目录中创建字体系列xml文件(必须定义android
和app
命名空间) Optional, but handy, create a font family xml file in
Resources/font
directory (bothandroid
andapp
namespaces must be defined)<!-- Resource/font/sushi.xml --> <?xml version="1.0" encoding="UTF-8" ?> <font-family xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"> <font android:fontStyle="normal" android:fontWeight="400" android:font="@font/tastysushi" app:fontStyle="normal" app:fontWeight="400" app:font="@font/tastysushi" />
注意:您还可以在字体系列中添加粗体和斜体
font
元素...,并为每个字体分配与相同字体系列不同的字体文件...Note: You can add bold and italic
font
elements also to the font family... and assign each one a different font file from the same family of fonts...在布局文件的顶部元素中添加
app:
命名空间:In top element of your layout file add the
app:
namespace:xmlns:app="http://schemas.android.com/apk/res-auto"
现在
fontFamily
属性必须用于使用app:
命名空间Now the
fontFamily
attribute must use to use theapp:
namespaceapp:fontFamily="@font/sushi"
注意:
@font/
可以引用实际的字体文件或基于xml的字体系列文件Note: The
@font/
can either reference the actual font file or the xml-based font family file注意:您还可以将字体嵌入样式中,因此不必用字体系列填充每个AXML元素...
Note: You can also embed the fonts in styles so you do not have to populate each AXML element with the font family...
这篇关于Xamarin.Android-android:fontFamily不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!