How to change fontFamily of TextView in Android

后端 未结 30 2464
感动是毒
感动是毒 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:41

    The valid value of android:fontFamily is defined in /system/etc/system_fonts.xml(4.x) or /system/etc/fonts.xml(5.x). But Device Manufacturer might modify it, so the actual font used by setting fontFamily value depends on the above-mentioned file of the specified device.

    In AOSP, the Arial font is valid but must be defined using "arial" not "Arial", for example android:fontFamily="arial". Have a qucik look at Kitkat's system_fonts.xml

        
        
            sans-serif
            arial
            helvetica
            tahoma
            verdana
        
        
            Roboto-Regular.ttf
            Roboto-Bold.ttf
            Roboto-Italic.ttf
            Roboto-BoldItalic.ttf
        
    
    

    //////////////////////////////////////////////////////////////////////////

    There are three relevant xml-attributes for defining a "font" in layout--android:fontFamily, android:typeface and android:textStyle. The combination of "fontFamily" and "textStyle" or "typeface" and "textStyle" can be used to change the appearance of font in text, so does used alone. Code snippet in TextView.java like this:

        private void setTypefaceFromAttrs(String familyName, int typefaceIndex, int styleIndex) {
        Typeface tf = null;
        if (familyName != null) {
            tf = Typeface.create(familyName, styleIndex);
            if (tf != null) {
                setTypeface(tf);
                return;
            }
        }
        switch (typefaceIndex) {
            case SANS:
                tf = Typeface.SANS_SERIF;
                break;
    
            case SERIF:
                tf = Typeface.SERIF;
                break;
    
            case MONOSPACE:
                tf = Typeface.MONOSPACE;
                break;
        }
        setTypeface(tf, styleIndex);
    }
    
    
        public void setTypeface(Typeface tf, int style) {
        if (style > 0) {
            if (tf == null) {
                tf = Typeface.defaultFromStyle(style);
            } else {
                tf = Typeface.create(tf, style);
            }
    
            setTypeface(tf);
            // now compute what (if any) algorithmic styling is needed
            int typefaceStyle = tf != null ? tf.getStyle() : 0;
            int need = style & ~typefaceStyle;
            mTextPaint.setFakeBoldText((need & Typeface.BOLD) != 0);
            mTextPaint.setTextSkewX((need & Typeface.ITALIC) != 0 ? -0.25f : 0);
        } else {
            mTextPaint.setFakeBoldText(false);
            mTextPaint.setTextSkewX(0);
            setTypeface(tf);
        }
    }
    

    From the code We can see:

    1. if "fontFamily" is set, then the "typeface" will be ignored.
    2. "typeface" has standard and limited valid values. In fact, the values are "normal" "sans" "serif" and "monospace", they can be found in system_fonts.xml(4.x) or fonts.xml(5.x). Actually both "normal" and "sans" are the default font of system.
    3. "fontFamily" can be used to set all fonts of build-in fonts, while "typeface" only provide the typical fonts of "sans-serif" "serif" and "monospace"(the three main category of font type in the world).
    4. When only set "textStyle", We actually set the default font and the specified style. The effective value are "normal" "bold" "italic" and "bold | italic".

提交回复
热议问题