How to change the font on the TextView?

前端 未结 16 737
清歌不尽
清歌不尽 2020-11-22 08:09

How to change the font in a TextView, as default it\'s shown up as Arial? How to change it to Helvetica?

相关标签:
16条回答
  • 2020-11-22 08:42

    Best practice is to use Android Support Library version 26.0.0 or above.

    STEP 1: add font file

    1. In res folder create new font resource dictionary
    2. Add font file (.ttf, .orf)

    For example, when font file will be helvetica_neue.ttf that will generates R.font.helvetica_neue

    STEP 2: create font family

    1. In font folder add new resource file
    2. Enclose each font file, style, and weight attribute in the element.

    For example:

    <?xml version="1.0" encoding="utf-8"?>
    <font-family xmlns:android="http://schemas.android.com/apk/res/android">
        <font
            android:fontStyle="normal"
            android:fontWeight="400"
            android:font="@font/helvetica_neue" />
    </font-family>
    

    STEP 3: use it

    In xml layouts:

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:fontFamily="@font/my_font"/>
    

    Or add fonts to style:

    <style name="customfontstyle" parent="@android:style/TextAppearance.Small">
        <item name="android:fontFamily">@font/lobster</item>
    </style>
    

    For more examples you can follow documentation:

    Working with fonts

    0 讨论(0)
  • 2020-11-22 08:42

    Maybe something a bit simpler:

    public class Fonts {
      public static HashSet<String,Typeface> fonts = new HashSet<>();
    
      public static Typeface get(Context context, String file) {
        if (! fonts.contains(file)) {
          synchronized (this) {
            Typeface typeface = Typeface.createFromAsset(context.getAssets(), name);
            fonts.put(name, typeface);
          }
        }
        return fonts.get(file);
      }
    }
    
    // Usage
    Typeface myFont = Fonts.get("arial.ttf");
    

    (Note this code is untested, but in general this approach should work well.)

    0 讨论(0)
  • 2020-11-22 08:46

    get font from asset and set to all children

    public static void overrideFonts(final Context context, final View v) {
        try {
            if (v instanceof ViewGroup) {
                ViewGroup vg = (ViewGroup) v;
                for (int i = 0; i < vg.getChildCount(); i++) {
                    View child = vg.getChildAt(i);
                    overrideFonts(context, child);
             }
            } else if (v instanceof TextView ) {
                ((TextView) v).setTypeface(Typeface.createFromAsset(context.getAssets(),"DroidNaskh.ttf"));// "BKOODB.TTF"));
            }
        } catch (Exception e) {
     }
     } 
    
    0 讨论(0)
  • 2020-11-22 08:47
    import java.lang.ref.WeakReference;
    import java.util.HashMap;
    
    import android.content.Context;
    import android.graphics.Typeface;
    
    public class FontsManager {
    
        private static FontsManager instance;
    
        private static HashMap<String, WeakReference<Typeface>> typefaces = new HashMap<String, WeakReference<Typeface>>();
    
        private static Context context;
    
        private FontsManager(final Context ctx) {
            if (context == null) {
                context = ctx;
            }
        }
    
        public static FontsManager getInstance(final Context appContext) {
            if (instance == null) {
                instance = new FontsManager(appContext);
            }
            return instance;
        }
    
        public static FontsManager getInstance() {
            if (instance == null) {
                throw new RuntimeException(
                        "Call getInstance(Context context) at least once to init the singleton properly");
            }
            return instance;
        }
    
        public Typeface getFont(final String assetName) {
            final WeakReference<Typeface> tfReference = typefaces.get(assetName);
            if (tfReference == null || tfReference.get() == null) {
                final Typeface tf = Typeface.createFromAsset(context.getResources().getAssets(),
                        assetName);
                typefaces.put(assetName, new WeakReference<Typeface>(tf));
                return tf;
            }
            return tfReference.get();
        }
    
    }
    

    This way, you can create a View which inherits from TextView and calls setTypeface on its constructor.

    0 讨论(0)
提交回复
热议问题