Set font for all textViews in activity?

后端 未结 8 2508
忘了有多久
忘了有多久 2020-11-28 06:07

Is it possible to set the font for all the TextViews in a activity? I can set the font for a single textView by using:

    TextView tv=(TextView)findViewByI         


        
8条回答
  •  孤独总比滥情好
    2020-11-28 06:37

    Best answers

    1. Setting custom font for one textView

    Typeface typeface = Typeface.createFromAsset(getContext().getAssets(), "Fonts/FontName.ttf");
    textView.setTypeface (typeface);
    

    2. Setting custom font for all textViews

    Create a JavaClass like below

    public class CustomFont extends android.support.v7.widget.AppCompatTextView {
    
        public CustomFont(Context context) {
            super(context);
            init();
        }
    
        public CustomFont(Context context, AttributeSet attrs) {
            super(context, attrs);
            init();
        }
    
        public CustomFont(Context context, AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
            init();
        }
    
        private void init() {
                Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "fonts/FontName.ttf");
                setTypeface(tf);
        }
    }
    

    And in your xml page

    
    
    ...
    
    />
    

    =>

        
    

提交回复
热议问题