Xamarin.Android - android:fontFamily doesn't work

后端 未结 1 1854
再見小時候
再見小時候 2021-01-16 01:20

I need to change textView font using android:fontFamily. The problem is that it gives me errors and I have no idea why

My code:

  

        
相关标签:
1条回答
  • 2021-01-16 01:40

    Support from only API-26 and above:

    • Add your font files in the Resources/font directory

    • For API-26+ only support using android:fontFamily is fine:

      android:fontFamily="@font/tastysushi"

    But... for API 16 through 28+, you need to setup/use the Android Support library

    Support from API-16+

    • Add the Xamarin.Android.Support.Compat package (v26 or higher)

    • 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)

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

    • Add the font files in the Resources/font directory

       |-Resources
       |  |-font
       |  |  |-tastysushi.ttf
      
    • 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"
      />
      

      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...

    • In top element of your layout file add the app: namespace:

      xmlns:app="http://schemas.android.com/apk/res-auto"

    • Now the fontFamily attribute must use to use the app: namespace

      app:fontFamily="@font/sushi"

    Note: The @font/ can either reference the actual font file or the xml-based font family file

    Note: You can also embed the fonts in styles so you do not have to populate each AXML element with the font family...

    0 讨论(0)
提交回复
热议问题