美文网首页
设置整个app的字体

设置整个app的字体

作者: 暗香浮 | 来源:发表于2018-08-13 14:40 被阅读0次

将字体包放到assets文件目录下,然后,取得路径,将下面方法在BaseActivity中调用

private void shareTTF () {
        String path = FileStorage.getInstance().getTtfDir() + File.separator + "2017_6_15_b68492fb3a975119.ttf";
        File file = new File(path);
         if(file.exists()) {
             Typeface typeface = Typeface.createFromFile(file);
             LayoutInflaterCompat.setFactory2(LayoutInflater.from(this),
                     new LayoutInflater.Factory2() {
                         @Override
                         public View onCreateView(String name, Context context, AttributeSet attrs) {
                             return null;
                         }

                         @Override
                         public View onCreateView(View parent, String name, Context context, AttributeSet attrs) {
                             AppCompatDelegate delegate = getDelegate();
                             View              view     = delegate.createView(parent, name, context, attrs);

                             if (view != null && view instanceof TextView) {
                                 ((TextView) view).setTypeface(typeface);
                             }

                             if (view != null && view instanceof EditText) {
                                 ((EditText) view).setTypeface(typeface);
                             }

                             if (view != null && view instanceof Button) {
                                 ((Button) view).setTypeface(typeface);
                             }
                             return view;
                         }
                     });
         }
    }

相关文章

网友评论

      本文标题:设置整个app的字体

      本文链接:https://www.haomeiwen.com/subject/ifcubftx.html