Using custom font in android TextView using xml

前端 未结 10 1083
执念已碎
执念已碎 2020-11-29 17:02

I have added a custom font file to my assets/fonts folder. How do I use it from my XML?

I can use it from code as follows:

TextView text =          


        
10条回答
  •  长情又很酷
    2020-11-29 17:58

    Here is example code that does this. I have the font defined in a static final variable and the font file is in the assets directory.

    public class TextViewWithFont extends TextView {
    
        public TextViewWithFont(Context context, AttributeSet attrs) {
            super(context, attrs);
            this.setTypeface(MainActivity.typeface);
        }
    
        public TextViewWithFont(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
            this.setTypeface(MainActivity.typeface);
        }
    
        public TextViewWithFont(Context context) {
            super(context);
            this.setTypeface(MainActivity.typeface);
        }
    
    }
    

提交回复
热议问题