Android - Using Custom Font

后端 未结 21 2027
别跟我提以往
别跟我提以往 2020-11-22 04:38

I applied a custom font to a TextView, but it doesn\'t seems to change the typeface.

Here is my code:

    Typeface myTypeface = Typeface         


        
21条回答
  •  情话喂你
    2020-11-22 05:25

    Since I was not satisfied with all the presented solutions on SO, I've come up with mine. It's based on a little trick with tags (i.e. you can't use tags in your code), I put the font path there. So when defining views, you can do either this:

    
    

    or this:

    
    
    
    

    Now you can either explicitly access / setup the view as:

    TextView textView = TextViewHelper.setupTextView(this, R.id.textViewHello1).setText("blah");
    

    or just setup everything via:

    TextViewHelper.setupTextViews(this, (ViewGroup) findViewById(R.id.parentLayout)); // parentLayout is the root view group (relative layout in my case)
    

    And what is the magic class you ask? Mostly glued from another SO posts, with helper methods for both activity and fragments:

    import android.app.Activity;
    import android.content.Context;
    import android.graphics.Typeface;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.TextView;
    
    import java.util.HashMap;
    import java.util.Map;
    
    public class TextViewHelper {
        private static final Map mFontCache = new HashMap<>();
    
        private static Typeface getTypeface(Context context, String fontPath) {
            Typeface typeface;
            if (mFontCache.containsKey(fontPath)) {
                typeface = mFontCache.get(fontPath);
            } else {
                typeface = Typeface.createFromAsset(context.getAssets(), fontPath);
                mFontCache.put(fontPath, typeface);
            }
            return typeface;
        }
    
        public static void setupTextViews(Context context, ViewGroup parent) {
            for (int i = parent.getChildCount() - 1; i >= 0; i--) {
                final View child = parent.getChildAt(i);
                if (child instanceof ViewGroup) {
                    setupTextViews(context, (ViewGroup) child);
                } else {
                    if (child != null) {
                        TextViewHelper.setupTextView(context, child);
                    }
                }
            }
        }
    
        public static void setupTextView(Context context, View view) {
            if (view instanceof TextView) {
                if (view.getTag() != null) // also inherited from TextView's style
                {
                    TextView textView = (TextView) view;
                    String fontPath = (String) textView.getTag();
                    Typeface typeface = getTypeface(context, fontPath);
                    if (typeface != null) {
                        textView.setTypeface(typeface);
                    }
                }
            }
        }
    
        public static TextView setupTextView(View rootView, int id) {
            TextView textView = (TextView) rootView.findViewById(id);
            setupTextView(rootView.getContext().getApplicationContext(), textView);
            return textView;
        }
    
        public static TextView setupTextView(Activity activity, int id) {
            TextView textView = (TextView) activity.findViewById(id);
            setupTextView(activity.getApplicationContext(), textView);
            return textView;
        }
    }
    

提交回复
热议问题