全局字体快速替换

直接在BaseActivity中添加以下代码:

public static Typeface typeface;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        if (typeface == null) {
            typeface = Typeface.createFromAsset(getAssets(), "fzkt.ttf");
        }
        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 || view instanceof Button || view instanceof EditText)) {
                    ((TextView) view).setTypeface(typeface);
                }
                return view;
            }

        });
    }

效率几乎无影响。

你可能感兴趣的:(安卓开发,setTypeface,安卓字体)