如何在RN for Android中禁用字体缩放?

Tal*_*boy 5 android react-native

只需allowFontScaling={false}在iOS上使用修复程序,但我不确定如何在Android上设置它.

此外,对于那些不熟悉RN并且从Android标签来到这里的人,我认为我不能轻易地从dp字体缩放或其他任何东西改变,所以有什么办法我可以在我的应用程序中以某种方式全局地做到这一点?

谢谢!

Nis*_*kar 9

请更新ManiApplication.java文件

import android.view.WindowManager;
import android.content.res.Configuration;
import android.content.Context;
import android.util.DisplayMetrics;
Run Code Online (Sandbox Code Playgroud)

super.onCreate();方法放完后adjustFontScale(getApplicationContext(),getResources().getConfiguration());

喜欢跟随

  @Override
  public void onCreate() {
    super.onCreate();
    adjustFontScale(getApplicationContext(),getResources().getConfiguration());
}
Run Code Online (Sandbox Code Playgroud)

最后添加以下功能

public void adjustFontScale(Context context, Configuration configuration) {
    if (configuration.fontScale != 1) {
        configuration.fontScale = 1;
        DisplayMetrics metrics = context.getResources().getDisplayMetrics();
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        wm.getDefaultDisplay().getMetrics(metrics);
        metrics.scaledDensity = configuration.fontScale * metrics.density;
        context.getResources().updateConfiguration(configuration, metrics);
    }
}
Run Code Online (Sandbox Code Playgroud)