Accessing a font under assets folder from XML file in Android

后端 未结 14 1179
暖寄归人
暖寄归人 2020-12-05 17:07

I am trying to do a application-wide font change and creating a style file to do so. In this file (below) I just want to change typeface value of TextAppearance style of And

14条回答
  •  既然无缘
    2020-12-05 18:07

    I took droid kid's answer and made it work with ANY font, just by typing the font filename directly into the XML:

    layout.xml

    
    
        
        
    
    
    

    Fonts.java

    public class Fonts {
    
        // use Weak so fonts are freed from memory when you stop using them
        private final static Map fonts = new WeakHashMap<>(5);
    
        /***
         * Returns a font at the given path within the assets directory.
         *
         * Caches fonts to save resources.
         *
         * @param context
         * @param fontPath Path to a font file relative to the assets directory, e.g. "fonts/Arial.ttf"
         * @return
         */
        public synchronized static Typeface getFont(Context context, String fontPath) {
            Typeface font = fonts.get(fontPath);
    
            if (font == null) {
                font = Typeface.createFromAsset(context.getAssets(), fontPath);
                fonts.put(fontPath, font);
            }
    
            return font;
        }
    }
    

    values/attrs.xml

    
    
        
            
            
    
            
                
                
            
        
    
    

    TextViewWithFont.java

    public class TextViewWithFont extends TextView {
        private int defaultDimension = 0;
        private int TYPE_BOLD = 1;
        private int TYPE_ITALIC = 2;
        private int fontType;
        private int fontName;
    
        public TextViewWithFont(Context context) {
            super(context);
            init(null, 0);
        }
        public TextViewWithFont(Context context, AttributeSet attrs) {
            super(context, attrs);
            init(attrs, 0);
        }
        public TextViewWithFont(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
            init(attrs, defStyle);
        }
        private void init(AttributeSet attrs, int defStyle) {
            // Load attributes
            final TypedArray a = getContext().obtainStyledAttributes(
                    attrs, R.styleable.TextViewWithFont, defStyle, 0);
            String fontPath = a.getString(R.styleable.TextViewWithFont_fontFilePath);
            fontType = a.getInt(R.styleable.TextViewWithFont_type, defaultDimension);
            a.recycle();
    
            if (fontPath != null) {
                setFontType(Fonts.getFont(getContext(), fontPath));
            }
        }
        private void setFontType(Typeface font) {
            if (fontType == TYPE_BOLD) {
                setTypeface(font, Typeface.BOLD);
            } else if (fontType == TYPE_ITALIC) {
                setTypeface(font, Typeface.ITALIC);
            } else {
                setTypeface(font);
            }
        }
    }
    

提交回复
热议问题