Xamarin.Android-android:fontFamily不起作用 [英] Xamarin.Android - android:fontFamily doesn't work

查看:479
本文介绍了Xamarin.Android-android:fontFamily不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用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 using fontFamily in your AXML layouts (otherwise you can programmatically assign the font family and use Activity subclasses)

    您的活动必须AppCompatActivity的子类,才能使基于layout/AXML的app:fontFamily属性起作用

    Your activity must be a subclass of AppCompatActivity in order for the layout/AXML-based app:fontFamily attribute to work

    Resources/font目录中添加字体文件

     |-Resources
     |  |-font
     |  |  |-tastysushi.ttf
    

  • 可选,但很方便,可以在Resources/font目录中创建字体系列xml文件(必须定义androidapp命名空间)

  • Optional, but handy, create a font family xml file in Resources/font directory (both android and app 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 the app: namespace

    app: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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆