How to change fontFamily of TextView in Android

后端 未结 30 2489
感动是毒
感动是毒 2020-11-22 01:05

So I\'d like to change the android:fontFamily in Android but I don\'t see any pre-defined fonts in Android. How do I select one of the pre-defined ones? I don\'

30条回答
  •  太阳男子
    2020-11-22 01:34

    If you want to use a TextView in so many places with same font family, extend the TextView class and set your font like this:-

    public class ProximaNovaTextView extends TextView {
    
        public ProximaNovaTextView(Context context) {
            super(context);
    
            applyCustomFont(context);
        }
    
        public ProximaNovaTextView(Context context, AttributeSet attrs) {
            super(context, attrs);
    
            applyCustomFont(context);
        }
    
        public ProximaNovaTextView(Context context, AttributeSet attrs, int defStyle) {
           super(context, attrs, defStyle);
    
           applyCustomFont(context);
        } 
    
        private void applyCustomFont(Context context) {
            Typeface customFont = FontCache.getTypeface("proximanova_regular.otf", context);
            setTypeface(customFont);
        }
    }
    

    And then use this custom class in xml for the TextView like this:-

       
    

提交回复
热议问题