如何防止我的应用中Android设备的显示尺寸缩放? [英] How do I prevent Android device Display size scaling in my app?

查看:138
本文介绍了如何防止我的应用中Android设备的显示尺寸缩放?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用React Native开发适用于iOS和Android的应用程序,并且试图阻止应用程序中特定于设备的显示缩放.

I'm developing an app with React Native for both iOS and Android, and I am trying to prevent device-specific scaling of the display in the app.

对于文本/字体大小缩放,将以下代码放在根级App.js文件中可解决iOS和Android的问题:

For text/font size scaling, putting the following code in the root-level App.js file solves the issue for both iOS and Android:

if (Text.defaultProps == null) {
    Text.defaultProps = {};
}
Text.defaultProps.allowFontScaling = false;

但是,Android设备具有以下仍在应用的显示尺寸设置:

However, Android devices have the following Display size setting that is still being applied:

我已尝试(未成功)为该问题拼凑出各种解决方案",这些解决方案是在以下问题的答案中找到的:

I've tried (unsuccessfully) to piece together a variety of "solutions" to this issue that I've found in answers to the following questions:

以编程方式更改Android N的系统显示大小

禁用应用程序或活动如果设置->则缩放.显示->显示尺寸更改为大或小

如何预防系统字体大小更改对android应用程序的影响?

我经常发现对扩展Activity类的BaseActivity类的引用.我的理解是,它是在该类的内部,我将在该类中编写一个方法(将其称为adjustDisplayScale),以更改从Resources获得的ContextConfiguration,然后我将在MainApplication.java文件中的super.onCreate()之后在onCreate()方法中调用adjustDisplayScale.

I've often found references to a BaseActivity class that extends the Activity class. My understanding is that it is inside of that class where I would be writing a method (let's call it adjustDisplayScale) to make changes to the Configuration of the Context that I get from Resources, and that then I would be calling adjustDisplayScale within the onCreate() method after super.onCreate() in the MainApplication.java file.

到目前为止,在此目录中,我只有两个文件-MainApplication.javaMainActivity.java.

As of now, in this directory I just have two files - MainApplication.java and MainActivity.java.

我已尝试按照以下说明创建新的模块和关联的包文件以实施adjustDisplayScale,但它不起作用: https://facebook.github.io/react-native/docs/text. html

I've attempted creating a new Module and associated Package file to implement adjustDisplayScale following these instructions and it did not work: https://facebook.github.io/react-native/docs/text.html

我试图像这样在onCreate()中放置实现adjustDisplayScale的功能,但是它不起作用:

I've attempted placing implementing the functionality of adjustDisplayScale within the onCreate() like this and it did not work:

@Override
public void onCreate() {
    super.onCreate();

    Context context = getApplicationContext();
    Resources res = context.getResources();
    Configuration configuration = res.getConfiguration();

    configuration.fontScale = 1f;
    DisplayMetrics metrics = res.getDisplayMetrics();

    WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
    wm.getDefaultDisplay().getMetrics(metrics);
    metrics.scaledDensity = 1f;
    configuration.densityDpi = (int) res.getDisplayMetrics().xdpi;
    context = context.createConfigurationContext(configuration);

    SoLoader.init(this, /* native exopackage */ false);
}

一个潜在的有希望的答案包括以下内容:

A potentially promising answer included the following:

protected override void AttachBaseContext(Context @base) {
    var configuration = new Configuration(@base.Resources.Configuration);
    configuration.FontScale = 1f;
    var config =  Application.Context.CreateConfigurationContext(configuration);
    base.AttachBaseContext(config);
}

但是当我尝试利用此功能时,出现了关于无法识别符号@base的错误.

But when I tried to utilize this, I got errors about not recognizing the symbol @base.

一些背景...我已经用JavaScript/React Native完成了该项目的99%的工作,而且我对ResourcesContextConfigurationDisplayMetrics之类的东西几乎一无所知.与Android开发相关,而我上一次用Java编写代码是10年前.我花了很多时间来努力解决这个问题,对我们的帮助将非常表示感谢.

Some background... I've done 99% of my work on this project in JavaScript / React Native and I have almost no understanding about things like Resources, Context, Configuration, and DisplayMetrics associated with Android development AND the last time I wrote code in Java was 10 years ago. I've spent a number of agonizing hours trying to figure this out and any help would be greatly appreciated.

ps.我很清楚可访问性设置的存在是有充分原因的,因此请避免在很多答案"中看到的错误提示,说明为什么我需要修复UI才能使用可访问性设置而不是禁用它们.

ps. I am well-aware that accessibility settings exist for a good reason so please spare me the diatribe I've seen in so many "answers" on why I need to fix my UI to work with accessibility settings rather than disable them.

推荐答案

您可以尝试以下代码(覆盖attachBaseContext).这将禁用"应用程序中的屏幕缩放.这是立即重新缩放整个屏幕的方法.

You can try following code (overriding attachBaseContext). This will "disable" the screen zoom in your app. This is the way to re-scale whole screen at once.

@Override
protected void attachBaseContext(final Context baseContext) {

    Context newContext;

    if(Build.VERSION.SDK_INT >= VERSION_CODES.N) {

        DisplayMetrics displayMetrics = baseContext.getResources().getDisplayMetrics();
        Configuration configuration = baseContext.getResources().getConfiguration();

        if (displayMetrics.densityDpi != DisplayMetrics.DENSITY_DEVICE_STABLE) {
            // Current density is different from Default Density. Override it
            displayMetrics.densityDpi = DisplayMetrics.DENSITY_DEVICE_STABLE;
            configuration.densityDpi = DisplayMetrics.DENSITY_DEVICE_STABLE;
            newContext = baseContext;//baseContext.createConfigurationContext(configuration);
        } else {
            // Same density. Just use same context
            newContext = baseContext;
        }
    } else {
        // Old API. Screen zoom not supported
        newContext = baseContext;
    }
    super.attachBaseContext(newContext);
}

在该代码上,我检查当前密度是否与设备的默认密度不同.如果不一样 我使用默认密度(而不是当前密度)创建一个新的上下文.然后,我附加此修改后的上下文.

On that code, I check if the current density is different from Device's default density. If they are different, I create a new context using default density (and not the current one). Then, I attach this modified context.

您必须在每个Activity上执行此操作.因此,您可以创建一个BaseActivity并在其中添加该代码.然后,您只需要更新您的活动即可扩展BaseActivity

You must do that on every Activity. So, you can create a BaseActivity and add that code there. Then, you just need to update your activities in order to extend BaseActivity

public class BaseActivity extends AppCompatActivity {
    @Override
    protected void attachBaseContext(final Context baseContext) {
        ....
    }
}

然后,在您的活动中:

public class MainActivity extends BaseActivity {
    // Since I'm extending BaseActivity, I don't need to add the code
    // on attachBaseContext again
    // If you don't want to create a base activity, you must copy/paste that
    // attachBaseContext code into all activities
}

我使用以下代码测试了此代码:

I tested this code with:

Log.v("Test", "Dimension: " + getResources().getDimension(R.dimen.test_dimension));

不同的屏幕缩放比例(使用该代码):

Different Screen Zoom (using that code):

2019-06-26 16:38:17.193 16312-16312/com.test.testapplication V/Test: Dimension: 105.0
2019-06-26 16:38:35.545 16312-16312/com.test.testapplication V/Test: Dimension: 105.0
2019-06-26 16:38:43.021 16579-16579/com.test.testapplication V/Test: Dimension: 105.0

不同的屏幕缩放比例(没有该代码):

Different Screen Zoom (without that code):

2019-06-26 16:42:53.807 17090-17090/com.test.testapplication V/Test: Dimension: 135.0
2019-06-26 16:43:19.381 17090-17090/com.test.testapplication V/Test: Dimension: 120.0
2019-06-26 16:44:00.125 17090-17090/com.test.testapplication V/Test: Dimension: 105.0

因此,使用该代码,无论缩放级别如何,我都可以得到相同的像素尺寸.

So, using that code, I can get the same dimension in pixels regardless the zoom level.

修改

这篇关于如何防止我的应用中Android设备的显示尺寸缩放?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆