Set specific font in a styles.xml

后端 未结 5 1707
滥情空心
滥情空心 2020-12-01 07:36

I\'m defining a style XML for my android app. I have some TTF files I want to use, how can I set the typeface to use those files as the font as opposed to the generic \"san

5条回答
  •  悲哀的现实
    2020-12-01 08:42

    Create a font dir on resource folder and paste your ttf font file. Then create a font resource XML and paste following lines.

        
    
        
        
    
    
    

    Now you can apply font as below. Also note attribute 'textStyle'

      
    
      
    

    If you want done from code use following, but minimum API level 26

    Typeface typeface = getResources().getFont(R.font. roboto_light_italic);
    textView.setTypeface(typeface);
    

    The Support Library 26.0 provides support to the Fonts in XML feature on devices running Android 4.1 (API level 16) and higher.

    Typeface typeface = ResourcesCompat.getFont(context, R.font. roboto_light_italic);
    

提交回复
热议问题