1. 程式人生 > >全域性字型快速替換

全域性字型快速替換

直接在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;
            }

        });
    }

效率幾乎無影響。