Using custom font in android TextView using xml

前端 未结 10 1069
执念已碎
执念已碎 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:57

    Create your customed TextView belong to the font you want to use. In this class, I use a static mTypeface field to cache the Typeface (for better performance)

    public class HeliVnTextView extends TextView {
    
    /*
     * Caches typefaces based on their file path and name, so that they don't have to be created every time when they are referenced.
     */
    private static Typeface mTypeface;
    
    public HeliVnTextView(final Context context) {
        this(context, null);
    }
    
    public HeliVnTextView(final Context context, final AttributeSet attrs) {
        this(context, attrs, 0);
    }
    
    public HeliVnTextView(final Context context, final AttributeSet attrs, final int defStyle) {
        super(context, attrs, defStyle);
    
         if (mTypeface == null) {
             mTypeface = Typeface.createFromAsset(context.getAssets(), "HelveticaiDesignVnLt.ttf");
         }
         setTypeface(mTypeface);
    }
    
    }
    

    In xml file:

    
    

    In java class:

    HeliVnTextView title = new HeliVnTextView(getActivity());
    title.setText(issue.getName());
    

提交回复
热议问题