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
目录下添加你的字体文件Support from only API-26 and above:
Add your font files in the
Resources/font
directory对于 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
的子类,以便基于布局/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屋!